FetchRequest完成时完成阻止/回调

时间:2013-01-16 13:57:07

标签: iphone ios objective-c callback nsfetchrequest

我的模型类中有一个名为FetchRequest的{​​{1}}。它获取了大量数据,因此在完成之前屏幕是空白的。在显示提取结果的ContentManager中,我想显示加载指示符,因此我希望在ViewController完成后收到回调并将其传递给FetchRequest停止加载指示器。这可能吗?

以下是ViewController类中的FetchRequest

ContentManager

我在- (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

我想像这样设置fetchrequest方法:

self.magazineRecipes = [[ContentManager defaultManager] recipesForMagazine];或其他什么,所以在- (NSArray*)recipesForMagazine:(void (^)(BOOL success))block我可以这样称呼它

viewcontroller

我不知道我是否正常思考,感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我会使viewController成为ContentManager类的委托。所以在ContentManager.h我会做类似的事情:

    @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执行@interface ViewController.h : UIViewController <ContentManagerDelegate>和实施中,您应该能够实施能够为您提供结果的didFetchResults:方法,并且在该方法中,您可以停止活动指标的制作动画。< / p>