简体   繁体   English

在视图控制器之间共享数据的单例或委托

[英]Singleton or delegation for shared data between view controllers

I have searched through SO looking for different ways to share data between view controllers. 我搜索了SO,寻找在视图控制器之间共享数据的不同方法。 I see that delegation to pass back data are the most common way ppl suggest, and that some ppl use singletons for the information to be available from anywhere in the app. 我看到委派回传数据是ppl建议的最常见方式,并且某些ppl使用单例来使信息可从应用程序中的任何位置获得。 I am not familiar with Core Data yet, but from what i've looked at so far, it seems like Core Data is similar to a Singleton in that from the ManagedObjectContext, you can access the data. 我还不熟悉Core Data,但是到目前为止,从ManagedObjectContext来看,Core Data似乎与Singleton类似,您可以访问数据。 So as long as you have a reference to that object, you can access that data (feel free to correct me if that understanding is wrong). 因此,只要您有对该对象的引用,就可以访问该数据(如果这种理解是错误的,请随时纠正我)。

In this scenario, I was wondering if delegation or singletons should be used. 在这种情况下,我想知道应该使用委托还是单例。 I basically have a tab bar controller with two tabs. 我基本上有一个带有两个标签的标签栏控制器。 One tab passes data back and forth between the views using delegation or assigning a property on the view to be presented. 一个选项卡使用委派或在要显示的视图上分配属性在视图之间来回传递数据。

The second tab is a summary view of the first tab. 第二个选项卡是第一个选项卡的摘要视图。 So on a completely different stack of views, I need that same data that was in Tab one. 因此,在完全不同的视图堆栈上,我需要使用表一中的相同数据。 In that scenario, should I be using a Singleton so it can be accessed from everywhere in the app? 在那种情况下,我应该使用Singleton以便可以从应用程序中的任何位置进行访问吗? Or is there a better solution? 还是有更好的解决方案?

Also, I'm wondering what you would do about archiving your data if your application is about to be closed. 另外,我想知道如果应用程序即将关闭,您将如何归档数据。 It seems like to me, if I did implement a singleton, when I receive the notification that my app is going to enter the background, I can look in my singleton and save the data. 在我看来,如果我确实实现了一个单例,当我收到我的应用即将进入后台的通知时,我可以查看单例并保存数据。 But if I don't use a singleton, how would I save the data when the app enters the background. 但是,如果我不使用单例,那么当应用程序进入后台时如何保存数据。 Do I put that saving code in the app delegate instead. 我是否应该将保存代码放入应用程序委托中。

I guess in the end I'm trying to understand what design pattern is better in this scenario for archiving and sharing data between the app. 我想最后我想了解在这种情况下哪种归档模式更好地归档和共享应用程序之间的数据。 Thanks! 谢谢!

I went through this nice tutorial on Core data: http://timroadley.com/2012/02/09/core-data-basics-part-1-storyboards-delegation/ 我仔细阅读了有关核心数据的这篇很好的教程: http : //timroadley.com/2012/02/09/core-data-basics-part-1-storyboards-delegation/

The approach used there is: your ManagedObjectContext is initialized for the first time in your AppDelegate. 此处使用的方法是:您的ManagedObjectContext首次在AppDelegate中初始化。 Then in my root view controller, i retrieve the ManagedObjectContext from the appdelegate. 然后在我的根视图控制器中,我从appdelegate检索ManagedObjectContext。 Then in prepareForSegue, pass the managedObjectContext to the pushed view Controllers. 然后在prepareForSegue中,将managedObjectContext传递给推送视图控制器。

The AppDelegate has the readonly accessor property for the __managedObjectContext, which is responsible for creating this singleton object for the first time. AppDelegate具有__managedObjectContext的只读访问器属性,该属性负责首次创建此单例对象。

In each view controller, I set up the fetchedResultsController on viewWillAppear with the entities I want from core data. 在每个视图控制器中,我都在ViewWillAppear上设置了fetchedResultsController,其中包含我想要的核心数据实体。 I then write directly to core data when the user hits the save button. 然后,当用户单击“保存”按钮时,我将直接写入核心数据。 In my particular application, i have an explicit "Cancel" button, because the user can make mistakes, or wipe out the editable text data accidentally. 在我的特定应用程序中,我有一个明确的“取消”按钮,因为用户可能会犯错误,或意外擦除可编辑的文本数据。 But you could persist changes immediately, upon each control's changed value, like in the "Settings" menu in iOS. 但是,您可以根据每个控件的更改值立即保留更改,就像在iOS中的“设置”菜单中一样。 I prefer the Cancel/Save approach simply because of the nature of my data. 我更喜欢取消/保存方法,原因仅在于数据的性质。 Some research is in order to see if you must re-save the whole object, or can only update individual fields in coredata. 进行一些研究是为了查看是否必须重新保存整个对象,或者只能更新coredata中的单个字段。

Core data pretty much takes care of your questions, if you read/write directly from managedObjectContext. 如果您直接从managedObjectContext读/写,那么核心数据几乎可以解决您的问题。 Data is persisted right away, and updated right away. 数据将立即保留并立即更新。 If you have data that is not persisted on purpose, then delegation is a good way to pass it between screens. 如果您有没有故意保留的数据,则委派是在屏幕之间传递数据的好方法。 If you need to notify a whole bunch of view controllers that you don't want to know about with non-persisted data, consider using NSNotificationCenter with object or with dictionary. 如果您需要通过非持久性数据通知您不希望知道的一堆视图控制器,请考虑将NSNotificationCenter与对象或词典一起使用。
Send and receive messages through NSNotificationCenter in Objective-C? 通过Objective-C中的NSNotificationCenter发送和接收消息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM