重启应用程序后为什么我的数据没有保存到Core Data?

时间:2014-06-06 11:28:37

标签: ios core-data ios7

我有viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    // create API request
    VKRequest * newsRequest = [VKApi requestWithMethod: @"newsfeed.get"
                                         andParameters: @{@"filters":@"post"}
                                         andHttpMethod: @"GET"];
    // execute request
    [newsRequest executeWithResultBlock:^(VKResponse *response) {

        NSArray *items = response.json[@"items"]; // json to array
        for (NSDictionary *item in items) {
            Post *post = [NSEntityDescription insertNewObjectForEntityForName:@"Post"
                                                       inManagedObjectContext:self.managedObjectContext];
            [post setSource_id: item[@"source_id"]];
            [post setText: item[@"text"]];
            [post setDate: [NSDate dateWithTimeIntervalSince1970: [item[@"date"] intValue] ] ];
            [post setType: item[@"type"]];
            [post setNumber_of_comments: @( [item[@"comments"][@"count"] intValue] ) ];
            [post setNumber_of_likes: @( [item[@"likes"][@"count"] intValue] ) ];
            NSError *error = nil;
            if (![self.managedObjectContext save: &error]) { // save data
                NSLog(@"error: %@", [error localizedDescription]);
            }
        }
        // request to coredata here
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *description = [NSEntityDescription entityForName: @"Post"
                                                       inManagedObjectContext: self.managedObjectContext];
        [request setEntity:description];
        NSError *requestError = nil;
        NSArray *resultArray = [self.managedObjectContext executeFetchRequest: request
                                                                        error: &requestError];
        NSLog(@"data: %@ /n", resultArray);

    } errorBlock: ^(NSError *error) {
        NSLog(@"%@", error);
    }];

}

首次启动后一切似乎都没问题(我在控制台中查看了所有数据),但是如果我重启应用程序,我看到50(我得到的帖子数量)行看起来像这样:

    "<Post: 0x8dbe110> (entity: Post; id: 0x8dac000 <x-coredata://EDD8C9C8-35E5-4C26-B6D2-8A9C00B077CF/Post/p1> ; data: <fault>)",
    "<Post: 0x8dbe170> (entity: Post; id: 0x8dacde0 <x-coredata://EDD8C9C8-35E5-4C26-B6D2-8A9C00B077CF/Post/p2> ; data: <fault>)",
    "<Post: 0x8dbe1c0> (entity: Post; id: 0x8dad970 <x-coredata://EDD8C9C8-35E5-4C26-B6D2-8A9C00B077CF/Post/p3> ; data: <fault>)",
    "<Post: 0x8dbe210> (entity: Post; id: 0x8dad980 <x-coredata://EDD8C9C8-35E5-4C26-B6D2-8A9C00B077CF/Post/p4> ; data: <fault>)",
    .........
    .........

在此之后,我有我的数据。 Obiusly这50个对象来自之前的开始,但为什么我看到它像<fault>

5次启动后,控制台会显示200 <fault>个数据,然后显示50条法线。

正常数据如下所示:

"<Post: 0x8d93050> (entity: Post; id: 0x8d94a70 <x-coredata://EDD8C9C8-35E5-4C26-B6D2-8A9C00B077CF/Post/p405> ; data: {\n    attachments = nil;\n    date = \"2014-06-05 23:39:23 +0000\";\n    \"number_of_comments\" = 0;\n    \"number_of_likes\" = 8;\n    source = nil;\n    \"source_id\" = \"-30509296\";\n    text = \"PROMO GALLERY Open Your Mind\";\n    type = post;\n})",

我做错了什么?

1 个答案:

答案 0 :(得分:1)

Core Data中的故障不是故障,而是故障,它只是Apple减少应用程序内存使用量的方法。您可以致电:

关闭酒店
[request setReturnsObjectsAsFaults:NO];