2012-06-02 5 views
1

업데이트 : 여기 실제 문제는 코어 데이터에 강하고 약한 속성 선언이 추가되어 새로운보기의 viewWillLoad와 viewDidLoad 사이에서 객체가 릴리스되는 결과를 가져 왔습니다.코어 데이터 multiple managedObjectContexts

저는 Tim Isted의 iOS 핵심 자료에 대한 책을 통해 작업하고 있으며 지금까지 잘 해왔습니다. 새로운 viewController가 두 번째 managedObjectContext - 아래의 editingContext를 사용하여 저장하기 전에 텍스트 필드의 변경 사항을 캡처하는 방법을 따르려고합니다. 이 시점에서

- (void)setCurrentPerson:(AWPerson *)aPerson { 
    if(!aPerson) 
    { 
     aPerson = [AWPerson randomPersonInManagedObjectContext:self.editingContext]; 
    } 
    else if([aPerson managedObjectContext] != self.editingContext) { 
     self.title = @"Edit person"; 
     aPerson = (id)[self.editingContext objectWithID:[aPerson objectID]]; 
    } 
    [...] 
} 

: 나는 aPerson에 대한 디버거에서 설명을 인쇄 할 때 aPerson = (id)[self.editingContext objectWithID:[aPerson objectID]]; 내가 대신

<AWPerson: 0x6b5de70> (entity: Person; id: 0x6b5bb60 <x-coredata://A6EC85F2-81A8-488F-B2E3-F82687C252A2/Person/p1> ; data: { 
    dateOfBirth = "1973-11-03 12:53:58 +0000"; 
    eyeColor = "(...not nil..)"; 
    firstName = Peter; 
    lastName = Dickens; 
    yearOfBirth = 1973; 

받아야 나는 <fault>는 값

<AWPerson: 0x6b609d0> (entity: Person; id: 0x6b5bb60 <x-coredata: 
//A6EC85F2-81A8-488F-B2E3-F82687C252A2/Person/p1> ; data: <fault>) 
를 대체하고 다음받을 경우

나는 무슨 일이 일어나고 있는지 정말로 볼 수 없다. 라인 aPerson이 값을 가지기 전에, 라인 이후에, 그들은 대체됩니다. 어떤 도움이라도 대단히 감사하겠습니다. 그것은 나에게 잘못 보이지 않는, 그러나 ...

감사, 스티브

+0

감사합니다. Jody & Rog. 당신은 잘못을 훨씬 더 잘 이해하고이를 통해 내 길을 도왔습니다. 나는 objectWithId가있는 선으로 아직도 고투하고있다 그러나 나는 거기 도착할 것이다. 그러나 오늘 저는 강하고 약한 부동산 신고를하는 것이 중요하다는 것을 배웠습니다. viewWillLoad와 viewDidLoad 사이의 값을 잃어 가고있었습니다. 디버거를 정말로 이해하기 시작한 지 몇 시간이 지난 후에, 나는 잘못된 속성 선언 (cue self hypathing)을 추적했습니다. 다음 번에 알게 될 것입니다. 다시 한 번 감사드립니다, Steve – Steve

답변

1

나는 그 책을 가지고 있지 않지만, 내가 도와하려고합니다. 그것은 내가 기대했던 것처럼 보입니다 (setCurrentPerson이 호출 될 때 aPerson이 다른 컨텍스트에서 살고 있다고 가정). 내가 게시 한 코드를 살펴 보겠습니다. 어쩌면 나는 당신의 질문이 무엇인지를 어떻게 결정할 수 있으며 어떻게 든 그 과정에서 답을 제공 할 수 있습니다. 내 의견은 코드에 주석으로 포함되어 있습니다.

- (void)setCurrentPerson:(AWPerson *)aPerson { 
    if(!aPerson) 
    { 
     // The aPerson object we were given is nil, so get one in the 
     // current editingContext. 
     aPerson = [AWPerson randomPersonInManagedObjectContext:self.editingContext]; 
    } 
    else if([aPerson managedObjectContext] != self.editingContext) { 
     // The aPerson object does not live in editingContext. However, apparently 
     // we want to make sure it lives in the editingContext. Remember, each managed 
     // that has been saved will have a permanent object-id. Any context can use 
     // the object-id to fetch an object into its own ManagedObjectContext. 
     self.title = @"Edit person"; 
     aPerson = (id)[self.editingContext objectWithID:[aPerson objectID]]; 

     // Now, aPerson will point to an object that lives in the MOC editingContext. 
     // However, if it was not previously registered in that MOC, it will be returned 
     // as a fault. This does not mean the object is not there, but this MOC has 
     // not loaded its data. As soon as you cal a method that needs the data, 
     // it will be faulted into memory. 

     // One way, is to access any of its properties. 
     NSLog(@"firstName = %@", [aPerson valueForKey:@"firstName"]); 

     // You can query an object to see if it is a fault. NO means it's 
     // not a fault, and the properties should all be in memory. YES, does not mean 
     // the data is NOT in memory though... it could be in a cache... 

     // You can manually fault the object into memory, but I would 
     // suggest you read all the documentation before using this, because it has 
     // side effects. Consider... 
     if ([aPerson isFault]) { 
      [self.editingContext refreshObject:aPerson mergeChanges:YES]; 
     } 

     // NOTE: In general, you just want the object management system itself 
     // to manage faults. However, if you really want to see that your objects 
     // are what they are supposed to be, you can do any of this to examine them. 
    } 
    [...] 
} 
0

코드에 아무런 문제가 없습니다.

fault은 저장소에 있지만 아직 페치되지 않은 개체에 대한 포인터를 나타내는 방법입니다.

주로 성능 향상을 위해 수행하고 즉시 firstName 즉 Person 객체의 속성에 액세스로, lastName 등 Coredata 검색 및 상점에서 해당 개체를 검색하고 NSLog 설명은 다음 무엇을 일치합니다 첫 번째 예에서 보았습니다.

+0

오, 저는 방금 Jog Hagins가 그의 답변에서 이미 이것을 지적했음을 눈치 챘습니다. – Rog