我在我的模型類ContentManager
中有一個FetchRequest
。它獲取了大量的數據,所以在完成之前屏幕一直是空白的。在顯示提取結果的ViewController
中,我想顯示一個加載指示符,因此我想在FetchRequest
完成時得到一個回調,並將其傳遞給ViewController
以停止加載指示符。這可能嗎?FetchRequest完成時完成塊/回調
下面是從ContentManager
類FetchRequest
:
- (NSArray*)recipesForMagazine
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSString* path = [[NSBundle mainBundle] pathForResource:@"magazine_recipe_guids" ofType:@"plist"];
NSArray* recipeGuids = [NSArray arrayWithContentsOfFile:path];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"guid IN %@",recipeGuids];
[request setPredicate:predicate];
NSSortDescriptor* sort = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
[request setSortDescriptors:[NSArray arrayWithObject:sort]];
[sort release];
NSError* error = nil;
NSArray* fetchResults = [managedObjectContext executeFetchRequest:request error:&error];
[request release];
return fetchResults;
}
在這裏,我將它設置在ViewController
self.magazineRecipes = [[ContentManager defaultManager] recipesForMagazine];
我想成立fetchrequest方法是這樣的:
- (NSArray*)recipesForMagazine:(void (^)(BOOL success))block
什麼的,所以在viewcontroller
我可以這樣稱呼它
self.magazineRecipes = [[CTContentManager defaultManager] recipesForMagazine:^(BOOL success) {
if (success) {
//stop activity indicator view
}
}];
我不知道我是否在正確的思維方式,感謝您的幫助提前!
你是我的英雄! – sixstatesaway
不客氣:) – codingNinja