1

我不知道为什么 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);
         }
     }

 }]; 
}
4

1 回答 1

0

您正在insertNewObjectForEntityForName:for 循环中执行 2 次插入 ( )。您应该使用 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);
    }
}];
于 2013-11-01T23:53:52.547 回答