2013-01-02 1 views
2

메신저로 작업하고 있으며 가져 오지 못하는 개념이 있습니다. 나는이 많은 관계로 하나 오브젝트 ACConversation 및 ACMessages을 관리했습니다, 그래서 내가 대화에 관련된 메시지를 저장하고 기반으로 그들을 다시 가져 오기 위해 노력하고있어 :관리 대상 객체가 가져올 때 다른 ID를 가짐

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.conversation = %@", self.conversation]; 

하지만 객체에 관련된 ID 다른 (그것은 메모리 주소 더 많은 것)과 위의 조건으로 아무것도 가져 나던 :

2013-01-01 18:32:13.727 CoreDataTest[9325:c07] The message to fetch: <NSManagedObject: 0x818f680> (entity: ACMessage; id: 0x8160630 <x-coredata:///ACMessage/t451CC5A3-5972-479C-BC7C-6B91CA4DA23C4> ; data: { 
    conversation = "0x818b770 <x-coredata:///ACConversation/t451CC5A3-5972-479C-BC7C-6B91CA4DA23C2>"; 
    sentDate = "2013-01-01 23:32:13 +0000"; 
    text = "Test message to Fetch through relationship!!!"; 
}) 
2013-01-01 18:32:13.728 CoreDataTest[9325:c07] The conversation associated to the message: <NSManagedObject: 0x818b710> (entity: ACConversation; id: 0x818b770 <x-coredata:///ACConversation/t451CC5A3-5972-479C-BC7C-6B91CA4DA23C2> ; data: { 
    draft = nil; 
    lastMessageSentDate = "2013-01-01 23:32:13 +0000"; 
    lastMessageText = "Test message to Fetch through relationship!!!"; 
    messages =  (
     "0x8160630 <x-coredata:///ACMessage/t451CC5A3-5972-479C-BC7C-6B91CA4DA23C4>" 
    ); 
    messagesLength = 0; 
    unreadMessagesCount = 0; 
    users =  (
     "0x818b610 <x-coredata:///ACUser/t451CC5A3-5972-479C-BC7C-6B91CA4DA23C3>" 
    ); 
}) 

2013-01-01 18:32:13.782 CoreDataTest[9325:c07] The conversation found is: <NSManagedObject: 0x835e440> (entity: ACConversation; id: 0x8199860 <x-coredata://6E4B40F2-F7B4-4275-BF6E-349101A1254F/ACConversation/p290> ; data: <fault>) 
2013-01-01 18:32:13.783 CoreDataTest[9325:c07] The conversation to find is: <NSManagedObject: 0x818b710> (entity: ACConversation; id: 0x835e2e0 <x-coredata://6E4B40F2-F7B4-4275-BF6E-349101A1254F/ACConversation/p296> ; data: { 
    draft = nil; 
    lastMessageSentDate = "2013-01-01 23:32:13 +0000"; 
    lastMessageText = "Test message to Fetch through relationship!!!"; 
    messages =  (
     "0x835e6d0 <x-coredata://6E4B40F2-F7B4-4275-BF6E-349101A1254F/ACMessage/p268>" 
    ); 
    messagesLength = 0; 
    unreadMessagesCount = 0; 
    users =  (
     "0x835ea20 <x-coredata://6E4B40F2-F7B4-4275-BF6E-349101A1254F/ACUser/p296>" 
    ); 
}) 

코드 아래 :

- (NSFetchedResultsController *)fetchedResultsController { 

    if (_fetchedResultsController) { 
     NSLog(@"_fetchedResultsController found: %@", _fetchedResultsController); 
     return _fetchedResultsController; 
    } 

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ACMessage"]; 
    NSError __autoreleasing *error = nil; 
    NSUInteger messagesCount = [managedObjectContext countForFetchRequest:fetchRequest error:&error]; 
    NSAssert(messagesCount != NSNotFound, @"-[NSManagedObjectContext countForFetchRequest:error:] error:\n\n%@", error); 
    if (messagesCount > MESSAGE_COUNT_LIMIT) { 
     [fetchRequest setFetchOffset:messagesCount-MESSAGE_COUNT_LIMIT]; 
    } 
    [fetchRequest setFetchBatchSize:10]; 
    [fetchRequest setSortDescriptors:@[[[NSSortDescriptor alloc] initWithKey:@"sentDate" ascending:YES]]]; 

    /* 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.conversation = %@", self.conversation]; 
    [fetchRequest setPredicate:predicate]; 
    //NSLog(@"Predicate: %@", predicate); 
    */ 

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"ACMessage"]; 

    _fetchedResultsController.delegate = self; 

    NSAssert([_fetchedResultsController performFetch:&error], @"-[NSFetchedResultsController performFetch:] error:\n\n%@", error); 
    return _fetchedResultsController; 
} 

- (void) setSimpleData 
{ 
    self.conversation = [NSEntityDescription insertNewObjectForEntityForName:@"ACConversation" inManagedObjectContext:managedObjectContext]; 
    self.conversation.lastMessageSentDate = [NSDate date]; 
    ACUser *user = [NSEntityDescription insertNewObjectForEntityForName:@"ACUser" inManagedObjectContext:managedObjectContext]; 
    [user setName: @"my user"]; 
    [user setElementId: @"kjh123k123"]; 

    [self.conversation addUsersObject:user]; 

    ACMessage *message = [NSEntityDescription insertNewObjectForEntityForName:@"ACMessage" inManagedObjectContext:managedObjectContext]; 
    self.conversation.lastMessageSentDate = message.sentDate = [NSDate date]; 
    self.conversation.lastMessageText = message.text = @"Test message to Fetch through relationship!!!"; 
    [self.conversation addMessagesObject:message]; 

    NSLog(@"The message to fetch: %@", message); 
    //NSLog(@"The user associated to the conversation: %@", user); 
    NSLog(@"The conversation associated to the message: %@", self.conversation); 

    NSError *error; 
    if (![[self managedObjectContext] save:&error]) { 
     //Make sure you handle this! 
     NSLog(@"ERROR: Can't save object context"); 
     exit(-1); 
    } 
} 

- (void) getSimpleData 
{ 
    NSError *error; 

    if (![[self fetchedResultsController] performFetch:&error]) { 
     // Update to handle the error appropriately. 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 

    NSInteger section = 0; 
    id sectionInfo = [[[self fetchedResultsController] sections] objectAtIndex:section]; 

    if ([sectionInfo numberOfObjects]>0) { 

     NSIndexPath *my0 = [NSIndexPath indexPathForRow:0 inSection:0]; 
     ACMessage *message = [self.fetchedResultsController objectAtIndexPath:my0]; 

     ACConversation *conv = message.conversation; 

     NSLog(@"The message fetch at index 0 is %@", message); 
     NSLog(@"The conversation found is: %@", conv); 
     NSLog(@"The conversation to find is: %@", self.conversation); 

    } else { 
     NSLog(@"No messages found!"); 
    } 
} 

내가 약간의 실종을 또는 ACConversation에서 ID를 설정하고 NSPredicate에서 대화에 관련된 모든 메시지를 찾으려면 사용해야합니까?

감사합니다. alex

답변

4

관리 대상 개체를 처음 생성 할 때 임시 개체는 objectID입니다. 변경 사항을 저장하면 영구 objectID으로 변경됩니다. 이것은 원래의 임시 값에서 이후 영구 값으로 변경되는 유일한 시간입니다. 당신이보고있는 차이점은이 변화가 일어날 때 예상되는 것과 정확히 일치합니다.

당신은 객체가이 같은 임시 ID가 있는지 여부를 확인할 수 있습니다

NSError *error = nil; 
BOOL success = [managedObjectContext obtainPermanentIDsForObjects:@[myManagedObject] error:&error]; 
:이 같은 changes-- 저장 한 전에 early-- 일이 또한 변환을 강제 할 수

BOOL hasTempID = [[myManagedObject objectID] isTemporaryID]; 

+0

+1 좋은 설명 –

+0

+1 고마워! 많이 도와 줬어. 백그라운드에서 저장하고 변경하기 전에 객체 ID를 유지하면 주 스레드 MOC가 이러한 객체를 가져 오지 못합니다 ... 임시 ID이기 때문입니다. – avishic

관련 문제