我有两个我正在处理的视图控制器,它们都从Base视图控制器继承
class A_ViewController:BaseViewController class B_ViewController:BaseViewController这两个VC都与我的firebase数据库进行了很大的交互。 所以我想要一个变量来跟踪所有下载的项目,这样两个VC就可以访问它而无需再次重新下载文件。
我试图在BaseViewController中放置一个变量名,以便访问两个A,B类
var allPostsDownloaded: [Post]!因此,在A_VC下载任何数据之前,它会检查此allPostsDownloaded变量,并在数据存在时从中加载。 如果它不存在,我追加到这个变量。 因此,当切换到B_VC时,可以完成相同的操作,并且在不需要时不会重新下载数据。
我不使用segue或protocal来传递数据的原因是两个VC与我的数据库交互得非常重要。 因此,尝试使用可变数据变得更加清晰,以便跟踪事物的位置。
但问题是,我
var allPostsDownloaded: [Post]!每当我在A和B VC(哪些是标签)之间切换时都会被调用。 这将导致变量为空并取消初始化。
我想我可以使用全局变量,但这不是一个好习惯吗? 任何人都可以解释为什么在新标签出现时重新调用它? 对此最好的解决方案。
I have two view controllers that I am working on which both inherits from a Base view controller
class A_ViewController: BaseViewController class B_ViewController: BaseViewControllerBoth of those VC interacts heavily with my firebase database. So I want a variable to keep track of all the downloaded items so those two VC can access it without the need to re-download the file again.
I tried to put a variable name in BaseViewController for the two A,B class to access
var allPostsDownloaded: [Post]!So before A_VC downloads any data, it checks for this allPostsDownloaded variable and loads from it if the data exists. If it doesnt exist, I append to this variable. So when switching to B_VC, the same can be done and no data is re-downloaded when not required.
Reason I am not using segue or protocal to pass data around is that the two VC interacts quite heavly with my database. So it was alot cleaner to try and have a mutural data varaible to keep track of where things are.
However, the problem is that i
var allPostsDownloaded: [Post]!gets called whenever I switch between A and B VC (Which are tabs). This will cause the variable to be empty and de-initialised.
I guess I could use a global variable instead but that is not good practice? Could anyone please explain why it gets re-called when new tab shows up? And the best solution for this.
最满意答案
正如@avi提到的创建新的单例类,那么你可以轻松传递和阅读。 以下是一个例子
class PersistentData { static let sharedInstance = PersistentData() // your global persistent variable var allPostsDownloaded = [Post]() }因此,在您的控制器中,您可以简单地阅读和设置如下
// read print(PersistentData.sharedInstance.allPostsDownloaded) // set new data. this just example, hence depends on your case PersistentData.sharedInstance.allPostsDownloaded.append(newPost) PersistentData.sharedInstance.allPostsDownloaded = posts还要记住,如果要在选项卡之间切换时想要读取新值,可以在viewDidAppear或viewWillAppear获取更新
as @avi mentioned create new singleton class, then you can pass and read easily. Below is an example
class PersistentData { static let sharedInstance = PersistentData() // your global persistent variable var allPostsDownloaded = [Post]() }So in your controllers you can simple read and set as below
// read print(PersistentData.sharedInstance.allPostsDownloaded) // set new data. this just example, hence depends on your case PersistentData.sharedInstance.allPostsDownloaded.append(newPost) PersistentData.sharedInstance.allPostsDownloaded = postsalso keep in mind that if you want to read new value when switching between tabs, you can get the updated in viewDidAppear or viewWillAppear
更多推荐
发布评论