• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何使用 CKModifyRecordsOperation.perRecordProgressBlock 更新进度

[复制链接]
菜鸟教程小白 发表于 2022-12-13 06:15:14 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

CKManager.h

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

CKManager.m

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

    completionHandler(self.progress);
}

- (void)saveRecordNSArray *)records withCompletionHandlervoid (^)(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 指示器的方法...

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];
                });
            }];



Best Answer-推荐答案


您应该在 saveRecord 调用中包含进度处理程序,如下所示:

- (void)saveRecordNSArray *)records withCompletionHandlervoid (^)(NSArray *, NSError *))completionHandler recordProgressHandlervoid (^)(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];
}

然后你可以像这样调用那个保存记录:

        [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 调用的一部分。 上面的代码未经我测试。所以我希望我没有错字

关于ios - 如何使用 CKModifyRecordsOperation.perRecordProgressBlock 更新进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29473336/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap