Parse.com:saveInBackgroundWithBlock - 未调用块

时间:2014-10-17 22:39:33

标签: ios push-notification parse-platform

我正在使用最新版本的Parse iOS SKD(v1.4.2)并让我的应用程序为iOS 8做好准备......

现在我遇到了以下问题:

如果用户订阅了推送频道,我使用saveInBackgroundWithBlock方法在订阅成功后显示提醒。 问题是现在永远不会调用成功的块!

订阅它没有任何问题 - 新频道立即出现在Parse.com后端。

所以我真的很困惑! ;)

有没有人遇到同样的问题,尤其是有解决方案?

    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation addUniqueObject:channel forKey:@"channels"];
    [currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {

            // Show success Alert
            UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:@"Success" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [successAlert show];

        } else {

            // Show error Alert
            UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [errorAlert show];

        }
    }];

更新:我玩过它,并注意到该块已被调用但我的警报未显示...

1 个答案:

答案 0 :(得分:1)

始终检查succeeded参数。与Apple API一样,它可以节省更多。我就是这样做的。此外,由于您的目标是iOS8,因此我强烈建议您使用新的UIAlertController API。

PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation addUniqueObject:channel forKey:@"channels"];
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    NSLog(@"%@", [error localizedDescription]); // DEBUG

    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController* alert;

        if (succeeded && !error) {
            // Success Alert
            alert = [UIAlertController alertControllerWithTitle:@"Success"
                                                        message:nil
                                                 preferredStyle:UIAlertControllerStyleAlert];
        } else {
            // Error Alert
            alert = [UIAlertController alertControllerWithTitle:@"Error"
                                                        message:nil
                                                 preferredStyle:UIAlertControllerStyleAlert];
        }

        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {
                                                                  [alert dismissViewControllerAnimated:YES completion:nil];
                                                              }];
        [alert addAction:defaultAction];

        [self presentViewController:alert animated:YES completion:nil];
    });
}];