简体   繁体   English

使用Restkit和核心数据的最佳实践

[英]Best practice of using restkit and core data

I am building an application that is constantly speaking to a webservice . 我正在构建一个不断与webservice对话的应用程序。 So posting and getting data all the time. 因此,一直都在发布和获取数据。 But all the data that I post and get should also be saved on the phone. 但是我发布和获取的所有数据也应该保存在手机上。

Problem 1 问题1

When I was looking through the examples. 当我浏览示例时。 I saw that they are loading all the data in the appDelegate . 我看到他们正在将所有数据加载到appDelegate For loading in small amount of data this is probably the best way to do it. 对于加载少量数据,这可能是最好的方法。 But in my case, is it still the best way or should I do this on ViewController Level . 但就我而言,这仍然是最好的方法还是应该在ViewController Level上执行此操作?

Problem 2 问题2

When I started the application I checked use core data this generated a lot of code for me in the appDelegate . 当我启动应用程序时,我检查了使用核心数据,这在appDelegate为我生成了很多代码。 But in this case I can't get to my managedObjectContext on viewController Level, right? 但是在这种情况下,我无法在viewController级别上viewController我的managedObjectContext ,对吗?

My question is now, what is the best way to get this properly done? 我的问题是,正确完成此任务的最佳方法是什么?

Kind regards 亲切的问候

Problem 1 问题1

When I was looking through the examples. 当我浏览示例时。 I saw that they are loading all the data in the appDelegate. 我看到他们正在加载appDelegate中的所有数据。 For loading in small amount of data this is probably the best way to do it. 对于加载少量数据,这可能是最好的方法。 But in my case, is it still the best way or should I do this on ViewController Level. 但就我而言,这还是最好的方法,还是应该在ViewController级别上执行此操作?

The use of an appDelegate for that is typical of sample code. 为此,典型的示例代码是使用appDelegate。

How to best deal with that depends strictly on your app. 如何最好地处理它完全取决于您的应用程序。 Encapsulating data transfer into your view controller is certainly a step forward compared to using the app delegate. 与使用应用程序委托相比,将数据传输封装到视图控制器中无疑是一个进步。 But, depending on your app, you might also devise a more specific data load controller to encapsulate all the relevant behavior. 但是,根据您的应用程序,您可能还会设计一个更具体的数据加载控制器来封装所有相关行为。 Indeed, I think the latter option works best even for relatively simple projects. 的确,我认为后一种选择即使对于相对简单的项目也最有效。

Problem 2 问题2

When I started the application I checked use core data this generated a lot of code for me in the appDelegate. 当我启动应用程序时,我检查了使用核心数据,这在appDelegate中为我生成了很多代码。 But in this case I can't get to my managedObjectContext on viewController Level, right? 但是在这种情况下,我无法在viewController级别上访问我的managedObjectContext,对吗?

If you look into the appDelegate.h file you should find properties for accessing core data from your controllers: 如果查看appDelegate.h文件,则应该找到用于从控制器访问核心数据的属性:

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

You could use that to access the managed object context through [UIApplication sharedApplication].delegate or you could factor that code out of the app delegate into your own model manager class. 您可以使用它通过[UIApplication sharedApplication].delegate访问托管对象上下文,也可以将该代码从应用程序委托中分解为自己的模型管理器类。 Again, this is strictly depending on your app. 同样,这完全取决于您的应用程序。 There are apps that use only 1 managed object contexts, apps that deal with more, etc. If your app does a very basic use of core data, you could leave this as it is. 有些应用程序仅使用1个托管对象上下文,而处理更多内容的应用程序等等。如果您的应用程序非常核心地使用了核心数据,则可以保持原样。

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

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