如何使用CKModifyRecordsOperation.perRecordProgressBlock更新进度 [英] How to update progress with CKModifyRecordsOperation.perRecordProgressBlock

查看:78
本文介绍了如何使用CKModifyRecordsOperation.perRecordProgressBlock更新进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与最近的线程更新MRProgress的进度有关。由于先前的线程(感谢Edwin!),我将Cloudkit查询从便捷API转换为CKOperations。因此,在使用CKModifyRecordsOperation保存记录时,我可以通过登录perRecordProgressBlock来查看记录的进度,这很棒。但是,我正在尝试将进度发送回视图控制器,但我不知道该怎么做。我为我的所有CloudKit方法创建了一个类-CKManager。我遇到的另一个问题是我不确定何时在VC中更新进度指示器(使用MRProgress框架)。是否在CKManager中调用保存操作之前,期间或之后调用它?是否应该递归调用它,直到进度== 1.0?这是我到目前为止的代码...除更新/设置进度指示器动画外,其他所有方法都可以正常工作(显示并显示0%,然后在保存操作完成后消失)。另外,我在CKManager类中使用了一个属性(双进度),我知道那是不正确的,但是我不确定该怎么做。而且我也不认为我在下面的CKManager类中声明/定义的回调方法是正确的。任何指导表示赞赏!

This is related to a recent thread Update progress with MRProgress. I converted my cloudkit queries from the convenience API to CKOperations as a result of previous thread (Thanks Edwin!). So while using CKModifyRecordsOperation to save a record, I can see the record's progress via logging in the perRecordProgressBlock, which is great. However, I'm trying to send this progress back to the viewcontroller and I cannot figure out how to do that. I have created a class for all of my CloudKit methods - CKManager. The other problem I'm having is that I'm unsure when to update the progress indicator (using MRProgress framework) in the VC. Do I call it before, during or after the save operations call in the CKManager? Should it be called recursively until the progress == 1.0? Here is the code I have so far...every works fine except for updating/animating the progress indicator (it appears and shows 0% and then disappears when the save operation is completed). Also, I'm using a property (double progress) in my CKManager class and I know that is incorrect, but I wasn't sure how else to do it. And I do not feel that the callback method I've declared/defined in my CKManager class below for this is correct either. Any guidance is appreciated!

CKManager.h

CKManager.h

@property (nonatomic, readonly) double progress;
- (void)recordProgressWithCompletionHandler:(void (^)(double progress))completionHandler;

CKManager.m

CKManager.m

    @property (nonatomic, readwrite) double progress;
    - (void)recordProgressWithCompletionHandler:(void (^)(double))completionHandler {

    completionHandler(self.progress);
}

- (void)saveRecord:(NSArray *)records withCompletionHandler:(void (^)(NSArray *, NSError *))completionHandler {

    NSLog(@"INFO: Entered saveRecord...");
    CKModifyRecordsOperation *saveOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:records recordIDsToDelete:nil];

    saveOperation.perRecordProgressBlock = ^(CKRecord *record, double progress) {
        if (progress <= 1) {
            NSLog(@"Save progress is: %f", progress);
            self.progress = progress;
        }
    };

    saveOperation.perRecordCompletionBlock = ^(CKRecord *record, NSError *error) {
        NSLog(@"Save operation completed!");
        completionHandler(@[record], error);
    };

    [self.publicDatabase addOperation:saveOperation];
}

Viewcontroller.m-这是从相机拍摄照片的方法并调用CKManager类以准备记录并将其保存到CK并显示MRProgress指示符...

Viewcontroller.m - this is from the method that takes the photo from the camera and calls the CKManager class to prepare the record and save it to CK as well as display the MRProgress indicator...

if (self.imageDataAddedFromCamera) {
            self.hud = [MRProgressOverlayView showOverlayAddedTo:self.myCollectionView animated:YES];
            self.hud.mode = MRProgressOverlayViewModeDeterminateCircular;
            self.hud.titleLabelText = UPLOADING_MSG;
            // prepare the CKRecord and save it
            [self.ckManager saveRecord:@[[self.ckManager createCKRecordForImage:self.imageDataAddedFromCamera]] withCompletionHandler:^(NSArray *records, NSError *error) {
                if (!error && records) {
                    NSLog(@"INFO: Size of records array returned: %lu", (unsigned long)[records count]);
                    CKRecord *record = [records lastObject];
                    self.imageDataAddedFromCamera.recordID = record.recordID.recordName;
                    NSLog(@"INFO: Record saved successfully for recordID: %@", self.imageDataAddedFromCamera.recordID);
                    [self.hud dismiss:YES];
                    [self.hud removeFromSuperview];
                    [self.imageLoadManager addCIDForNewUserImage:self.imageDataAddedFromCamera]; // update the model with the new image
                    // update number of items since array set has increased from new photo taken
                    self.numberOfItemsInSection = [self.imageLoadManager.imageDataArray count];
                    [self updateUI];
                } else {
                    NSLog(@"Error trying to save the record!");
                    NSLog(@"ERROR: Error saving record to cloud...%@", error.localizedDescription);
                    [self.hud dismiss:YES];
                    [self.hud removeFromSuperview];
                    [self alertWithTitle:YIKES_TITLE andMessage:ERROR_SAVING_PHOTO_MSG];
                }
            }];
            // where does this call belong?
            [self.ckManager recordProgressWithCompletionHandler:^(double progress) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSLog(@"Updating hud display...");
                    [self.hud setProgress:progress animated:YES];
                });
            }];


推荐答案

您应该在saveRecord调用中包括进度处理程序,例如

You should include the progress handler in your saveRecord call like this:

- (void)saveRecord:(NSArray *)records withCompletionHandler:(void (^)(NSArray *, NSError *))completionHandler recordProgressHandler:(void (^)(double))progressHandler {

    NSLog(@"INFO: Entered saveRecord...");
    CKModifyRecordsOperation *saveOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:records recordIDsToDelete:nil];

    saveOperation.perRecordProgressBlock = ^(CKRecord *record, double progress) {
        if (progress <= 1) {
            NSLog(@"Save progress is: %f", progress);
            progressHandler(progress)
        }
    };

    saveOperation.perRecordCompletionBlock = ^(CKRecord *record, NSError *error) {
        NSLog(@"Save operation completed!");
        completionHandler(@[record], error);
    };

    [self.publicDatabase addOperation:saveOperation];
}

然后您可以像这样调用保存的记录:

Then you can call that save record like this:

        [self.ckManager saveRecord:@[[self.ckManager createCKRecordForImage:self.imageDataAddedFromCamera]] withCompletionHandler:^(NSArray *records, NSError *error) {
            if (!error && records) {
                NSLog(@"INFO: Size of records array returned: %lu", (unsigned long)[records count]);
                CKRecord *record = [records lastObject];
                self.imageDataAddedFromCamera.recordID = record.recordID.recordName;
                NSLog(@"INFO: Record saved successfully for recordID: %@", self.imageDataAddedFromCamera.recordID);
                [self.hud dismiss:YES];
                [self.hud removeFromSuperview];
                [self.imageLoadManager addCIDForNewUserImage:self.imageDataAddedFromCamera]; // update the model with the new image
                // update number of items since array set has increased from new photo taken
                self.numberOfItemsInSection = [self.imageLoadManager.imageDataArray count];
                [self updateUI];
            } else {
                NSLog(@"Error trying to save the record!");
                NSLog(@"ERROR: Error saving record to cloud...%@", error.localizedDescription);
                [self.hud dismiss:YES];
                [self.hud removeFromSuperview];
                [self alertWithTitle:YIKES_TITLE andMessage:ERROR_SAVING_PHOTO_MSG];
            }
        }, recordProgressHandler:^(double progress) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Updating hud display...");
                [self.hud setProgress:progress animated:YES];
            });
        }];

因此更新进度的代码是saveRecord调用的一部分。
上面的代码未经我测试。所以我希望我没有打错字

So that the code for updating the progress is part of your saveRecord call. The code above is not tested by me. So I hope I made no typo's

这篇关于如何使用CKModifyRecordsOperation.perRecordProgressBlock更新进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆