Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

objective c - NSPrivateQueueConcurrencyType Not saving properly

The following method gets called in order to populate my Core-Data after AFNetworking fetches information from my app server.

The information seems to be perfectly working as when the table is updated I can see the new information being updated in the UITableView.

Now the problem that I have is that even tho I can see the information ( after it has been fetches from the server, stored into Core-data and refetches to display in my UITableView) If I then go and close my app and re open it, the information is not there anymore.

It seems as if the information is not persistent and the problem seems to be the thread. given that if I remove the thread option in my method everything works fine.

What am I missing?? I have tried most things that I came across but I can't seem to find a solution.

NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc]
                                        initWithConcurrencyType:NSPrivateQueueConcurrencyType];
childContext.parentContext = managedObjectContext;
myModel.context = childContext;


   [childContext performBlock:^{
// ... Lots Controller logic code that then calls the class myModel where all my Core-Data save methods are

    // Sort Wall Pictures
                    if ( [dataHolder[@"verb"] isEqualToString:@"addWallPicture"] ) {
                        data = @{  @"resourceID":dataHolder[@"_id"][@"$id"],
                                   @"resourceName":dataHolder[@"details"][@"resourceName"],
                                   @"author":@{ @"id":dataHolder[@"userId"][@"$id"],
                                                @"username":dataHolder[@"details"][@"authorName"] },
                                   @"likesNumber":@0,
                                   @"likesPeople":@[]
                                   };

                        [myModel saveSocialWall:data date:date verb:dataHolder[@"verb"] code:dataHolder[@"_id"][@"$id"] myUser:myUser];
                        continue;
                    }
[childContext save:&error];
}];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to save the main context as well at some point, e.g. after saving the child context.

Saving the child context saves only to the main context, and saving the main context saves to the store file.

Like this (written on the phone, there will be syntax errors):

// ...
[childContext save:&error];
[mainContext performBlock:^{
    [mainContext save:&error];
}];

In Swift 2.0 that would be:

do {
    try childContext.save()
    mainContext.performBlock { 
        do {
            try mainContext.save()
        } catch let err as NSError {
            print("Could not save main context: (err.localizedDescription)")
        }
    }
} catch let err as NSError {
    print("Could not save private context: (err.localizedDescription)")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...