vendredi 31 juillet 2015

Capturing and using CKQueryCursor results using blocks?

HELP! I don't understand blocks in objective C. Here is my code.

-(void)loadStudentControls1n2Picker
{
    postCursor = nil ;
    [self getStudent];
}

-(void)getStudent
{
    __block CKQueryCursor *cursorCursor = nil;
if (postCursor == nil ) {
    CKDatabase *publicDatabase = [[CKContainer containerWithIdentifier:@"blah.com"] publicCloudDatabase];
    NSPredicate *predicatex = [NSPredicate predicateWithFormat:@"iBeaconConfig = %@", iBeaconsConfirmed.giReferenceID];
    CKQuery *query = [[CKQuery alloc] initWithRecordType:@"SingleBeaconsDB" predicate:predicatex];

    CKQueryOperation *queryOp =[[CKQueryOperation alloc] initWithQuery:query];
    queryOp.desiredKeys = @[@"record.recordID.recordName",@"Owner",@"iBeaconMajor",@"iBeaconMinor",@"OwnerID"];
    queryOp.resultsLimit = 4;

    queryOp.recordFetchedBlock = ^(CKRecord *results)
    {
        if (results[@"Owner"] != nil) {
            [globalPickerConfirmed addRecord:results[@"Owner"]:results.recordID.recordName];
        }
        [iBeaconSingleConfirmed addObject:results];
    };

    queryOp.queryCompletionBlock = ^(CKQueryCursor *cursor, NSError *operationError)
    {
        AAPLPostManagerErrorResponse error = [self handleError:operationError];
        NSLog(@"in block error & cursor %ld %@",(long)error,cursor);
        cursorCursor = [CKQueryCursor alloc];
        cursorCursor = cursor;
        postCursor = cursorCursor;
        NSLog(@"queryOp  postCursor %@",postCursor);
        [self.delegate performSelectorOnMainThread:@selector(refreshPicker:)  withObject:@"Teacher" waitUntilDone:YES];
    };
    [publicDatabase addOperation:queryOp];
} else {
    CKDatabase *publicDatabase = [[CKContainer containerWithIdentifier:@"blah.com"] publicCloudDatabase];
    CKQueryOperation *cursorOp = [[CKQueryOperation alloc] initWithCursor:postCursor];
    cursorOp.desiredKeys = @[@"record.recordID.recordName",@"Owner",@"iBeaconMajor",@"iBeaconMinor",@"OwnerID"];
    cursorOp.resultsLimit = 4;

    cursorOp.recordFetchedBlock = ^(CKRecord *results)
    {
        if (results[@"Owner"] != nil) {
            [globalPickerConfirmed addRecord:results[@"Owner"]:results.recordID.recordName];
        }
        [iBeaconSingleConfirmed addObject:results];
    };

    cursorOp.queryCompletionBlock = ^(CKQueryCursor *cursor, NSError *operationError)
    {
        AAPLPostManagerErrorResponse error = [self handleError:operationError];
        cursorCursor = [CKQueryCursor alloc];
        cursorCursor = cursor;
        postCursor = cursorCursor;
        NSLog(@"in block error & cursor & postCursor %ld %@ %@",(long)error,cursor,postCursor);
        [self.delegate performSelectorOnMainThread:@selector(refreshPicker:)  withObject:@"Teacher" waitUntilDone:YES];
    };
    [publicDatabase addOperation:cursorOp];
}
}

Now first time in to loadStudentContoller1n2Picker it works, downloads 3 records. Second time in via a button action it works, gets the next 4 records. i.e. the cursor is correctly saved and passed on. However third time in it resets itself and I get a copy of the first three records. Indeed it seems very inconsistent, sometimes its worse, first time it works, second time does nothing... Ahhhhhh...

Aucun commentaire:

Enregistrer un commentaire