0

空のテーブル ビューに追加しようとすると、このエラーが発生します。行 0 をセクション 0 に挿入しますが、更新後にセクション 0 に行が 0 行しかありません。何かご意見は?

  dispatch_queue_t fetchQ = dispatch_queue_create("Blog Fetcher", NULL);
  dispatch_async(fetchQ, ^{
                      //pull this call out to Blog+twitter later saving should only take 
                      //place in the model!

                        [document.managedObjectContext performBlock:^
                        {
                            NSArray* resultsArray = [self.tweets objectForKey:@"results"]; 

                            for (NSDictionary* internalDict in resultsArray)
                            {

                            User *user = [NSEntityDescription 
                                                  insertNewObjectForEntityForName:@"User"inManagedObjectContext:self.context];
                                user.username =[internalDict objectForKey:@"from_user_name"];
                                [self.context save:nil];

                                self.list = [self.list arrayByAddingObject:user];
                                NSLog(@" indexpath set here %i",self.list.count-1);
                                NSIndexPath *newIndexpath =[NSIndexPath indexPathForRow:self.list.count-1  inSection:0];
                                [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
                            }
                        }];

              });
            dispatch_release(fetchQ);

}
4

1 に答える 1

0

私の最初の考えは、更新をメインスレッドにディスパッチする必要があるということです。実装の詳細はわかりませんが[self.context save:nil]、メインスレッドに移動する必要があるかもしれません。

NSArray* resultsArray = [self.tweets objectForKey:@"results"];
NSMutableArray *users = [NSMutableArray arrayWithCapacity:resultsArray.count];
NSMutableArray *indexPaths =  [NSMutableArray arrayWithCapacity:resultsArray.count];
NSInteger row = self.list.count;

for (NSDictionary* internalDict in resultsArray)
{
    User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User"inManagedObjectContext:self.context];
    user.username =[internalDict objectForKey:@"from_user_name"];
    [self.context save:nil];

    [users addObject:user];

    NSLog(@" indexpath set here %i", row);
    NSIndexPath *newIndexpath =[NSIndexPath indexPathForRow:row inSection:0];
    row++;

    [indexPaths addObject:newIndexpath];
}

dispatch_async(dispatch_get_main_queue(), ^{
    self.list = [self.list arrayByAddingObjectsFromArray:users];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
};
于 2012-05-25T23:15:59.720 に答える