2012-04-25 21 views
0

我使用CoreData並執行以下命令返回的對象的數量:CoreData的形式findAll返回一個對象,但簡單fetchRequest不會返回0對象

[Contact findAll]; 

然而,當我建立一個簡單的NSFetchrequest像這樣,我得到0個對象。

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

    fetchRequest.entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.context]; 
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES]]; 

    self.fetchedResultsController = [[NSFetchedResultsController alloc] 
            initWithFetchRequest:fetchRequest 
            managedObjectContext:self.context 
            sectionNameKeyPath:nil 
            cacheName:nil]; 

    NSError *error; 
    NSArray *array = [self.context executeFetchRequest:fetchRequest error:&error]; 

請告訴我我在做什麼錯在這裏。由於我沒有Predicate集,我期望獲得相同數量的對象,但是我得到一個包含0個對象和error = nil的數組。請注意0​​不是零。

+0

你得到一個空陣列,還是你什麼都沒有得到? – Jim 2012-04-25 11:55:00

+0

@Jim:我得到一個空數組。我更新了答案,使其更加清晰。 – Besi 2012-04-25 11:56:17

+0

定義了'findAll'方法在哪裏?我不認爲它是核心數據的標準部分。 – Jim 2012-04-25 12:13:07

回答

2

如果您使用的是RESTkit,我猜RESTKit設置了自己的Core Data上下文/存儲,當您手動執行時,您使用的是Apple的Xcode模板提供的默認值。停止使用self.context並從RESTKit獲取上下文,或者使用REST Kit的便捷方法之一,例如objectsWithFetchRequest:

+0

這似乎是完全正確的,只要我切換上下文,它就像一個魅力。 – Besi 2012-04-25 12:40:58

1

從0.10開始的RestKit維護一個每線程對象存儲。如果RestKit還沒有完成你的數據(例如:你的RKRequestQueue已經完成,但它仍在處理你的映射),這可能會很棘手。

您可以訪問一個外國商店與活動記錄類別,即:

NSManagedObjectContext *restContext = [[[RKObjectManager sharedManager] objectStore] managedObjectContextForCurrentThread]; 
NSArray *allTexts = [TextEntity findAllInContext:restContext]; 

希望這有助於!