繁体   English   中英

iOS-解析:findObjectsInBackgroundWithBlock完成

[英]iOS - Parse: findObjectsInBackgroundWithBlock with completion

我正在使用findObjectsInBackgroundWithBlock获取数据,但是当所有数据收集findObjectsInBackgroundWithBlock ,我必须使用数据。 我不知道如何实现回调或任何能够在检索到所有数据并可供我使用后提醒我的方法。 有什么方法可以提醒您?

码:

[query findObjectsInBackgroundWithBlock:^(NSArray* array, NSError* error)
{
    if(!error)
    {
        if([array count] > 0)
        {
            PFObject* relationship = [array objectAtIndex:0];

            if([relationship.objectId length] > 0)
            {
                if([[relationship objectForKey:@"initiatedBy"] isEqualToString:parseID]) // relationship initiated by current user -youLike
                {
                    [youLike addObject:relationship];
                    NSLog(@"youLike added");
                }
                else
                {
                    [likeYou addObject:relationship];
                    NSLog(@"likeYou added");
                }
            }
            else NSLog(@"Custom error when cycling through user relationships: objectId is nil");
            }
        else
        {
            NSLog(@"Custom error when cycling through user relationships: Relationship at index %d could not be found in database", i);
        }
    }
    else
    {
        NSLog(@"Error when cycling through user relationships: %@", error);
    }
}

您需要使用Blocks

声明Block (在这里选择 block types

typedef void (^YourBlock)(NSArray *array, NSError *error);

在方法中添加blockfindObjectsInBackgroundWithBlock

- (void) yourMethod:(YourBlock)block {
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        //Do your things here if you want change something.
        //For instance: convert types, convert errors.
        block(array, error); //Is called when every thing is retrieve.
    }
}

在应用程序中调用您的方法

SomeClass *someClass = [SomeClass alloc]init];
[[object getScoresFromParse:^(NSArray *array, NSError *error) {
    //Everything complete here.
}];

您可以使用块。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{


    [query findObjectsInBackgroundWithBlock:^(NSArray* array, NSError* error)
    {
        if(!error)
        {

            if([array count] > 0)
            {
                PFObject* relationship = [array objectAtIndex:0];

                if([relationship.objectId length] > 0)
                {

                    // It will be called when Parse finishes.
                    dispatch_async(dispatch_get_main_queue(), ^{

                        if([[relationship objectForKey:@"initiatedBy"] isEqualToString:parseID]) // relationship initiated by current user -youLike
                        {
                            [youLike addObject:relationship];
                            NSLog(@"youLike added");
                        }
                        else
                        {
                            [likeYou addObject:relationship];
                            NSLog(@"likeYou added");
                        }

                        // [...yourcode...]

                    });

                }
                else {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        NSLog(@"Custom error when cycling through user relationships: objectId is nil");
                    });
                }

            }

        }

    }];


}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM