完成对FetchRequest完成的阻止/回调 [英] Completion block/callback on FetchRequest completion
问题描述
我在名为 ContentManager
的模型类中有一个 FetchRequest
。它获取了大量的数据,所以屏幕是空白的,直到它完成。在显示获取结果的 ViewController
中,我想显示一个加载指示器,所以我想获得一个回调当 FetchRequest
已完成并传递到
ViewController
以停止加载指示器。这是可能吗?
I have a FetchRequest
in my model class named ContentManager
. It fetches quite a lot of data, so the screen is blank until it's completion. In the ViewController
that displays the fetched results, I would like to show a loading indicator, so I would like to get a callback when the FetchRequest
has completed and pass that to the ViewController
to stop the loading indicator. Is this possible?
这是来自 ContentManager $ c $的
FetchRequest
c> class:
Here is the FetchRequest
from the ContentManager
class:
- (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
Here I set it up in the ViewController
self.magazineRecipes = [[ContentManager defaultManager] recipesForMagazine];
我想像这样设置fetchrequest方法:
I want to set up the fetchrequest method like this:
- (NSArray *)recipesForMagazine :( void(^)(BOOL success))block
或者什么,所以在 viewcontroller
我可以这样调用
- (NSArray*)recipesForMagazine:(void (^)(BOOL success))block
or something, so in the viewcontroller
I can call it like this
self.magazineRecipes = [[CTContentManager defaultManager] recipesForMagazine:^(BOOL success) {
if (success) {
//stop activity indicator view
}
}];
我不知道我是否在正确的思维方式,谢谢你提前帮助!
I don't know if I'm at all in the right way of thinking, thanks for your help in advance!
推荐答案
我会使viewController成为 ContentManager
类的委托。所以在 ContentManager.h
中我会做类似的:
I would make the viewController a delegate of the ContentManager
class. So in the ContentManager.h
I would do something like:
@protocol ContentManagerDelegate()
-(void) didFetchResults:(NSArray *) results;
-(void) didResultsFail: (NSError *) error;
@end
@interface ContentManager : <SuperClass Name>
-(id) initWithDelegate: (id<ContentManagerDelegate>) delegate;
@property (nonatomic, strong) id<ContentManagerDelegate> delegate;
@end
并在实施中:
-(id) initWithDelegate: (id<ContentManagerDelegate>) delegate
{
self = [super init];
if(self)
{
_delegate = delegate;
}
return self;
}
并在 recipesForMagazine
方法,你可以使用委托 [_ delegate didFetchResults:fetchResults]
,你可以实现一个方法来将错误传递给委托。在您的 ViewController.h
do @interface ViewController.h:UIViewController< ContentManagerDelegate>
能够实现 didFetchResults:
方法,将给你的结果,在该方法中,你可以停止活动指示器的动画。
and in your recipesForMagazine
method you can use the delegate [_delegate didFetchResults: fetchResults]
, you can implement a method to pass errors to the delegate if you want as well. In your ViewController.h
do @interface ViewController.h : UIViewController <ContentManagerDelegate>
and in the implementation you should be able to implement the didFetchResults:
method that will give you the results and in that method you can stop the activity indicator from animating.
这篇关于完成对FetchRequest完成的阻止/回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!