0

코어 데이터는 물론 GCD도 사용하고 있습니다. 핵심 데이터가 스레드 안전하지 않다는 것을 알고 있으므로 부모로서 mainContext와 함께 하위 관리 객체 컨텍스트 (tempContext)를 생성합니다. MainContext는 데이터를 저장할 PCS를 가지고있다. 내가하고 있어요 것은 : 페이스 북에서관리 대상 객체 ID를 자식 관리 객체 자식 컨텍스트가있는 GCD의 NSMutableArray에 추가 할 수 없습니다.

  1. 풀 게시물
  2. 저장 I는 각 게시물을 처리하는 동안 경우, 포스트라는 새로운 관리 객체 생성을 업데이트하고 그것을
  3. 을 저장하여 코어 데이터에 각 게시물 추가적인 처리가 필요하다. post.obectID를 NSMutableArray of Managed Object IDS에 추가한다. 이 NSMutableArray는 다른 프로세스에서 게시물 업데이트를 완료하는 데 사용됩니다. 별도의 프로세스가 동일한 tempContext에 없으므로 개체 ID를 사용하고 있으므로 개체 ID를 사용하여 핵심 데이터에서 게시물을 가져옵니다.

프로세스가 데이터를 가져오고 store.data에 저장되는 것을 봅니다. 실제 데이터베이스 파일의 내용을 보려면 Base라는 제품을 사용합니다. 하지만 게시물의 개체 ID를 저장할 수 없습니다. 코드에서 볼 수 있듯이 NSLog를 사용하여 게시물의 개체 ID를 인쇄하고 볼 수 있으므로 개체 ID가 있음을 알 수 있습니다. 또한 [tempContext save]를하고 있고 [mainContext save :]를하고 있음을 보여주는 코드도 있으므로 게시물에 임시 객체 ID가 아닌 영구적 ID가 있어야합니다. 다시 말하지만, 데이터는 코어 데이터의 실제 파일 내에 있지만 가변 배열 수는 여전히 0입니다.

NSMutableArray에 객체 ID를 저장하는 것이 잘못되었습니다. 코드에서 메인 큐에서도 [mutableArray addObject : p.objectID]를 실행하려고 시도한 곳을 주석으로 표시하지만 중요하지는 않습니다.

- (void)processPosts { 

NSLog(@"-- Begin processPosts --"); 

dispatch_async(_processPostsQ, ^{ 

    for (NSDictionary * info in _posts) 
    { 
     NSManagedObjectContext * tempContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; 
     tempContext.parentContext = mainMOContext; 

     // If Post exists, rewrite over top of it. otherwise create a new one 
     NSFetchRequest  * fetchRequest = [[NSFetchRequest alloc] init]; 
     NSEntityDescription * entity  = [[mainMOModel entitiesByName] objectForKey:@"Post"]; 

     [fetchRequest setEntity:entity]; 

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"fb_post_id = %@", 
            [info objectForKey:@"post_id"]]; 

     [fetchRequest setPredicate:predicate]; 

     NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"fb_post_id" ascending:YES]; 
     [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sd]]; 

     NSError * error = nil; 
     NSArray * fetchResults = [tempContext executeFetchRequest:fetchRequest error:&error]; 

     Post * p = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:tempContext]; 

     if (fetchResults.count > 0) 
     { 
      p = [fetchResults objectAtIndex:0]; 
     } 

     NSDictionary * appData = [[NSDictionary alloc]init]; 
     appData = [info objectForKey:@"app_data"]; 

     p.fb_actor_id  = [info objectForKey:@"actor_id"]; 
     p.fb_app_data  = [self archivedData:[info objectForKey:@"app_data"]]; 
     p.fb_attachment  = [self archivedData:[info objectForKey:@"attachment"]]; 
     p.fb_created_time = [info objectForKey:@"created_time"]; 
     p.timestamp   = [info objectForKey:@"updated_time"]; 
     p.fb_attribution = NULL_TO_NIL([info objectForKey:@"attribution"]); 
     p.fb_message  = NULL_TO_NIL([info objectForKey:@"message"]); 
     p.fb_type   = NULL_TO_NIL([info objectForKey:@"type"]); 
     p.fb_post_id  = [info objectForKey:@"post_id"]; 
     p.fb_likes_info  = [self archivedData:[info objectForKey:@"like_info"]]; 
     p.fb_comments_info = [self archivedData:[info objectForKey:@"comment_info"]]; 
     p.fb_parent_post_id = NULL_TO_NIL([info objectForKey:@"parent_post_id"]); 
     p.fb_permalink  = NULL_TO_NIL([info objectForKey:@"permalink"]); 
     p.fb_photo_data  = nil; 
     p.fb_place   = NULL_TO_NIL([info objectForKey:@"places"]); 
     p.fb_source_id  = [info objectForKey:@"source_id"]; 
     p.social_network = @"facebook"; 
     p.fkPostToFriend = [[FriendStore sharedStore]findFriendWithFBUID:[info objectForKey:@"source_id"] withMOContext:tempContext]; 

     [tempContext save:&error]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      NSError * error; 
      [mainMOContext save:&error]; 
     }); 


     if (appData.count > 0) 
     { 
      if ([[appData objectForKey:@"photo_ids"] count] > 0) 
      { 
       NSLog(@" -- photos need to be loaded for postID: %@",p.objectID); 
       //dispatch_async(dispatch_get_main_queue(), ^{ 
        [_postsNeedingPhotos addObject:p.objectID]; 
       //}); 
      } 
     } 
    } 

    NSLog(@"ProcessPosts: num of posts needing photos: %d",_postsNeedingPhotos.count); 
    dispatch_async(_getPhotosQ, ^{ 
     [self loadPhotoData]; 
    }); 
    NSLog(@"-- End processPosts --"); 
}); 

} 당신의 도움에 미리

감사 :

여기 내 코드입니다!

답변

0

나는 바보입니다. _postsNeedingPhotos를 초기화하는 것을 잊었습니다 :

_postsNeedingPhotos = [[NSMutableArray alloc] init];

이제는 모든 것이 잘 실행됩니다. :)

관련 문제