解析saveInBackgroundWithBlock

时间:2015-05-25 16:26:45

标签: multithreading parse-platform save

我是客观的新手。 当我按下保存按钮时,我正在尝试设置保存按钮。并执行saveInBackgroundWithBlock。

***由于未捕获的异常'NSInternalInconsistencyException'终止应用程序,原因:' - [UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished]只能从主线程调用。'

这是我的代码 `

    saveButton.enabled = NO;

PFQuery *medQuery = [PFQuery queryWithClassName:medicineClassName];

    // Retrieve the object by id
    [medQuery getObjectInBackgroundWithId:self.receiveObjectId block:^(PFObject *medObject, NSError *error) {

    // Now let's update it with some data
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [medObject setObject:[formatter numberFromString:self.medIDTF.text] forKeyedSubscript:@"medID"];

        // Medicine image
        NSData *imageData = UIImageJPEGRepresentation(self.medImage.image, 0.8f);
        NSMutableString *tmpString = [[NSMutableString alloc] init];
        tmpString = [self filenameEncoderFromString:(NSMutableString *)self.medMerEngNameTF.text];

        NSString *fileName = [NSString stringWithFormat:@"%@.png", tmpString];

        PFFile *imageFile = [PFFile fileWithName:fileName data:imageData];
        /*
        if (imageFile == nil) {

            [medObject setObject:[UIImage imageNamed:@"Ironman head.png"] forKey:@"medImage"];
        } else {

            [medObject setObject:imageFile forKeyedSubscript:@"medImage"];
        }*/
        [medObject setObject:imageFile forKey:@"medImage"];

        // other Medicine columns
        tmpString = [self filenameEncoderFromString:(NSMutableString *)self.medMerEngNameTF.text
                     ];
        [medObject setObject:tmpString forKeyedSubscript:@"medMerEngName"];
        [medObject setObject:self.medMerChiNameTF.text forKeyedSubscript:@"medMerChiName"];
        [medObject setObject:self.medScienceTF.text forKeyedSubscript:@"medScienceName"];
        [medObject setObject:self.medCategoryTF.text forKeyedSubscript:@"medCategory"];

        // show progress when saving
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDAnimationFade;
        hud.labelText = @"Uploading...";
        [hud showAnimated:YES whileExecutingBlock:^{
            float progress = 0.0f;
            while (progress < 1.0f) {

                progress += 0.01f;
                hud.progress = progress;
                usleep(10000);
            }
        }];

        // update medicine to parse
        [medObject saveInBackgroundWithBlock:^(BOOL successed, NSError *error) {        
            if (successed == YES) {
                  // show success message and hide the saving progress
                  MBProgressHUD *hudd = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                  hudd.mode = MBProgressHUDModeCustomView;
                  hudd.labelText = @"Upload Successfully";
                  hudd.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark@2x.png"]];
                    [hudd showAnimated:YES whileExecutingBlock:^{
                        //
                        [self setUneditable];
                        [hud hide:YES];
                    } completionBlock:^{
                        //                MedicineTableViewController *mtvd = [[MedicineTableViewController alloc] init];
                        //                [mtvd download];
                        // Notify table view to reload the medicines from parse cloud
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshParse" object:self];

                    }];

`

谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

您收到此错误的原因是您尝试从后台线程影响UI。 saveInBackgroundWithBlock在后台线程上执行块 - 从而使UI代码不安全且不稳定。在您的块中,您应该使用GCD(Grand Central Dispatch)包装您的HUD更新。您还需要以相同的方式进行NSNotification调用:

dispatch_async(dispatch_get_main_queue(), ^{

            MBProgressHUD *hudd = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hudd.mode = MBProgressHUDModeCustomView;
            hudd.labelText = @"Upload Successfully";
            hudd.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark@2x.png"]];
            [hudd showAnimated:YES whileExecutingBlock:^{

            [self setUneditable];
            [hud hide:YES];

        });

dispatch_async(dispatch_get_main_queue(), ^{

            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshParse" object:self];

        });

那应该适合你。