0

I am trying to return a bool value from a class method I call that has a block in it. I get the error, Incompatible block pointer types sending.... How would I get around this? I just want to know if the class method I call completes with or without error...

+ (BOOL)saveSelectedDepartmentsToParse:(NSMutableDictionary *)dictionary {

    NSArray *array = [dictionary allKeysForObject:@"YES"];
    NSMutableArray *trimmedArray = [[NSMutableArray alloc] init];

    for (NSString *string in array) {

        NSString *final = [string removeAllInvalidCharacters];
        [trimmedArray addObject:final];
    }

    NSLog(@"Array = %@", trimmedArray);

    PFInstallation *currentInstallation = [PFInstallation currentInstallation];

    [currentInstallation removeObjectForKey:@"channels"];
    [currentInstallation addObjectsFromArray:trimmedArray forKey:@"channels"];
    [currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

        if (error == nil) {

            NSLog(@"Parse Save Succeeded");

            [self saveDepartmentsDictionary:dictionary];
        }
        else {

            NSLog(@"Parse Save Failed, %@", error.localizedDescription);
        }
    }];
}
Jon Erickson
  • 1,876
  • 4
  • 30
  • 73

2 Answers2

1

I just want to know if the class method I call

This is a misunderstanding of how asynchronous code works. When you are supplying a block to saveInBackgroundWithBlock:, that code is not executed straight away. It's executed at some later point by the Parse framework, and whichever part of Parse that does so would get the return value if the block were defined to have one, which it isn't. Your block isn't executed at the point at which you write it, so you can't return anything at the point at which you write it.

Your code isn't calling the block, and you can't return values to your code from it. It doesn't make sense to do so. If another part of your code wants to know when the saving has finished, you'll need to use some other mechanism than return values, such as calling a method from your block, posting a notification, or Key-Value Observing.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • I am referring to the method I wrote, it is a class method. I know saveInBackground is not a class method, as it says so it plain letters that is is a instance method. – Jon Erickson Mar 13 '13 at 02:08
  • Ah, I see. The rest of my answer stands. The class method returns before the block gets executed. – Jim Mar 13 '13 at 02:14
1

From the block keyword InBackground:

[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (error == nil) {
        NSLog(@"Parse Save Succeeded");
        [self saveDepartmentsDictionary:dictionary];
    } else {
        NSLog(@"Parse Save Failed, %@", error.localizedDescription);
    }
}];

I guess the block is called asynchronously.

If you want to get the result, you can wait here until the block is executed, but this make the saveInBackgroundWithBlock useless.

So NSNotification may be better:

[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (error == nil) {
        NSLog(@"Parse Save Succeeded");
        [self saveDepartmentsDictionary:dictionary];
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationParseSaveSucceeded object:nil];
    } else {
        NSLog(@"Parse Save Failed, %@", error.localizedDescription);
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationParseSaveFailed object:nil];
    }
}];
Jason Lee
  • 3,200
  • 1
  • 34
  • 71