简体   繁体   English

如何使用Core Data预加载数据

[英]How to preload data with Core Data

I created a core data app for the first time. 我是第一次创建核心数据应用。 It's running good. 运行良好。 I created all my objects with data with a web service (with AFNetworking). 我使用Web服务(通过AFNetworking)用数据创建了所有对象。 My objects are all built. 我的对象全部建成。 So, now I want to know what is the best way to preload data. 因此,现在我想知道什么是预加载数据的最佳方法。 (webservice>Core data>My views) For the time being, it's simple but absolutely no user-friendly and that is this : (webservice>核心数据>我的看法)暂时,这很简单,但是绝对不友好,那就是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [ManagedMember loadDataFromWebService];
    [ManagedLesson loadDataFromWebService];
    [ManagedThematic loadDataFromWebService];
    [ManagedNew loadDataFromWebService];
    [ManagedProject loadDataFromWebService];

    return YES;
}

loadDataFromWebService is a function which call two consecutive functions. loadDataFromWebService是一个调用两个连续函数的函数。 (Delete all data in Core data objects and re-create objects which working well) So, I want to know where I can call this 5 functions ? (删除Core数据对象中的所有数据并重新创建运行良好的对象)因此,我想知道在哪里可以调用这5个函数? Is it possible to have a loader in the splash screen like many iOS apps ? 是否可以像许多iOS应用程序一样在启动屏幕中安装加载程序? Or created an intermediate view which load the data. 或创建一个加载数据的中间视图。
Many thanks. 非常感谢。

Never, never, ever load data like this in didFinishLaunchingWithOptions: . 永不,永不永不将这样的数据加载到didFinishLaunchingWithOptions: This method should do the minimum necessary and then return, as quickly as possible. 此方法应尽必要的最少工作,然后尽快返回。 Putting slow stuff there can cause a bad user experience, and iOS can shut your app down forcibly if it doesn't exit that function quickly enough. 在其中放慢速度的东西可能会导致不良的用户体验,如果iOS应用没有足够快地退出该功能,它可能会强行关闭您的应用。

As D-eptdeveloper suggests, viewDidLoad is a better place to trigger loading code, but it's still not great. 正如D-eptdeveloper所建议的那样, viewDidLoad是触发加载代码的更好的地方,但是它仍然不是很好。 Ideally, you trigger your data loading somewhere early on in the app, and individual view controllers just request the data as necessary (with the understanding that the data might not be available yet, and so might have to show some sort of progress indicator). 理想情况下,您可以在应用程序的某个早期位置触发数据加载,并且各个视图控制器仅在必要时请求数据(要了解数据可能尚不可用,因此可能必须显示某种进度指示器)。

A suitable place/time to trigger data loading is after the initial UI has been shown to the user. 在向用户显示了初始UI之后,触发数据加载的合适位置/时间。 This reduces the potential problem whereby triggering data loading at an earlier time actually slows down the appearance of the UI (not a good user experience). 这减少了潜在的问题,即在较早的时间触发数据加载实际上会减慢UI的外观(不是良好的用户体验)。

It's ok to trigger syncing in the AppDelegate (in the applicationDidBecomeActive: method), because it's the first place where your code is executed. 在AppDelegate(在applicationDidBecomeActive:方法中)触发同步是可以的,因为它是执行代码的第一个位置。 Just make sure you do all the syncing in a background thread. 只要确保您在后台线程中完成所有同步即可。

This topic is quite general, but you can find good examples about sync strategies online, eg How To Synchronize Core Data with a Web Service – Part 1 . 这个主题很笼统,但是您可以找到有关在线同步策略的良好示例,例如, 如何将核心数据与Web服务同步-第1部分

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

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