为什么Core Data没有正确保存属性?

时间:2013-11-01 22:38:16

标签: ios objective-c uitableview core-data

我不知道为什么Core Data没有正确保存我的属性。它正确保存内容,但不会排列值(我有两列)。它给了我16行而不是我想要的8行。 “urls”列应与“album_names”对齐,而不是按下。

来自模拟器的sqlite文件的屏幕截图: http://imgur.com/tTOiqtf

TBAppDelegate.h文件:

@property(strong, retain) NSData *photoUrls

TBAppDelegate.m文件:

- (void)requestAlbums 
{
[FBRequestConnection startWithGraphPath:@"me/albums/" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
 {
     TBAppDelegate *delegate = (TBAppDelegate *)[[UIApplication sharedApplication] delegate];

     managedObjectContext = [delegate managedObjectContext];

     if (error)
     {
     }

     NSArray *collection = (NSArray *)[result data];

     for (int i=0; i < collection.count; i++)
     {
         NSData *album_names = [NSKeyedArchiver archivedDataWithRootObject:collection];

         Everything *everything = (Everything *)[NSEntityDescription insertNewObjectForEntityForName:@"Everything" inManagedObjectContext:managedObjectContext];

         everything.album_names = album_names;

         NSArray *album = [collection objectAtIndex:i];

         NSString *photoQuery = [NSString stringWithFormat:@"%@/photos", [album valueForKey:@"id"]];

         [FBRequestConnection startWithGraphPath:photoQuery completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
         {
             NSArray *photoResult = (NSArray *)[result data];

             self.photoUrls = [NSKeyedArchiver archivedDataWithRootObject:photoResult];

        }];

         Everything *urls = (Everything *)[NSEntityDescription insertNewObjectForEntityForName:@"Everything" inManagedObjectContext:managedObjectContext];

         urls.urls = self.photoUrls;

         NSError *coreError;

         if (![managedObjectContext save:&coreError])
         {
             NSLog(@"%@", coreError);
         }
     }

 }]; 
}

1 个答案:

答案 0 :(得分:0)

您在for循环中正在进行2次插入(insertNewObjectForEntityForName:)。您应该使用url更新它,而不是插入新对象。

给它一个机会。用下面的代码替换你的第二个请求块。

[FBRequestConnection startWithGraphPath:photoQuery completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
    NSArray *photoResult = (NSArray *)[result data];

    self.photoUrls = [NSKeyedArchiver archivedDataWithRootObject:photoResult];
    everything.urls = self.photoUrls;

    NSError *coreError;

    // You can put the save somewhere else so that it doesn't get called 8 times in your case
    if (![managedObjectContext save:&coreError])
    {
        NSLog(@"%@", coreError);
    }
}];