코어 데이터 엔티티 'MyGalleryPhoto'에서 쿼리하는 NSFetchedResultsController가 있습니다.MagicalRecord + Core objectID 문자열 비교가 작동해도 컨텍스트간에 개체를 찾을 수 없습니다.
일부 개체를 삭제하려고하는데 문제가 발생합니다. MagicalRecord를 사용하고 있습니다. 내보기에서 잘 작동합니다 코드에서 내 원래 시도입니다. 코드가 실행되는 시점에 객체는 fetchedResultsController에 표시되기 때문에 분명히 존재합니다.
[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext) {
for (MyGalleryPhoto *myGalleryPhoto in [self.fetchedResultsController.fetchedObjects objectsAtIndexes: self.selectedIndexes]) {
NSError *error = nil;
MyGalleryPhoto *localMyGalleryPhoto = (MyGalleryPhoto *) [localContext existingObjectWithID: myGalleryPhoto.objectID error: &error];
NSLog(@"error: %@:%@", [error localizedDescription], [error userInfo]);
NSLog(@"mygp: %@", [localMyGalleryPhoto description]);
[localMyGalleryPhoto deleteInContext: localContext];
}
} completion:^(void){
}];
이 코드는 작동하지 않습니다. myGalleryPhoto 항목을 찾을 수 없으며 반환 된 오류 : "작업을 완료 할 수 없습니다. (코코아 오류 133000.)"또한 MR_inContext, 그냥 existingObjectWithId 호출하여 사용하려고 : 오류 :.
난장판을 많이 뒤쫓아 가면서이 나쁜 프랑켄슈타인의 괴물을 생각해 냈습니다. 엔티티에서 모든 레코드를 가져 와서 ObjectID의 문자열 표현을 비교합니다. 이것은 잘 작동합니다. 왜? 오늘 GitHub에서 다운로드 한 MagicalRecord 사본, XCode 최신, 최신 SDK 등을 사용하고 있습니다. the documentation에 기재된
[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext) {
NSArray *allMyGalleryPhotos = [MyGalleryPhoto findAllInContext: localContext];
for (MyGalleryPhoto *myGalleryPhoto in [self.fetchedResultsController.fetchedObjects objectsAtIndexes: self.selectedIndexes]) {
MyGalleryPhoto *myGalleryPhotoToDelete = nil;
for (MyGalleryPhoto *existingMyGalleryPhoto in allMyGalleryPhotos) {
NSString *existingURLString = [[existingMyGalleryPhoto.objectID URIRepresentation] absoluteString];
NSString *URLString = [[myGalleryPhoto.objectID URIRepresentation] absoluteString];
NSLog(@"ExistingURLString: %@", existingURLString);
NSLog(@"URLString: %@", URLString);
if ([URLString isEqualToString: existingURLString]) {
myGalleryPhotoToDelete = existingMyGalleryPhoto;
}
}
if (myGalleryPhotoToDelete) [myGalleryPhotoToDelete deleteInContext: localContext];
}
} completion:^(void){
}];