开发者

CoreData - doubt about when saving

I'm starting in coreData, and I have a doubt.

I have an App that reads data from a server, I parse the data, and get as NSDictionary of Objects.

To save the data to coreData, i do the following:

    for (NSDictionary *activityData in arrayWithResult){
        [CompanyActivity createActivityWithInfoFromServer:activityData inManagedObjectContext:self.managedObjectContext];
    }

    if (![self.managedObjectContext save:&error])
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

This reads about 300 records. The method 'createActivityWithInfoFromS开发者_如何转开发erver:' checks if there is any record with that name. If so, updates the data, if not, creates one.

The problem is that, while the "for" cycle is running, if i interact with the user interface, it stops saving in core data, sometimes, not always. Why?

If I take the SAVE inside the cycle, the problem disappears.

What should I do?

Thanks all,

RL


Depending on what your reasoning is to still allow the user to interact with the UI, you could either disable the UI for that time, or create a new thread that handles the piece of code that you posted.

As for the reasoning why it only fails to save sometimes, I assume that sometimes it doesn't have to create as many new objects and finishes sooner, and can save before you interact with the UI. If you save inside the for loop than you are saving everything after looking at each item which is fairly inefficient.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜