Ios 如何使用CKModifyRecordsOperation.perRecordProgressBlock更新进度
这与最近的一个线程有关。由于上一个线程(感谢Edwin!),我将cloudkit查询从便利API转换为CKOperations。因此,在使用CKModifyRecordsOperation保存记录时,我可以通过登录perRecordProgressBlock来查看记录的进度,这非常好。但是,我正在尝试将此进度发送回viewcontroller,但我不知道如何执行此操作。我已经为我的所有CloudKit方法创建了一个类-CKManager。我遇到的另一个问题是,我不确定何时在VC中更新进度指示器(使用MRProgress框架)。我是在CKManager中的save operations调用之前、期间还是之后调用它?是否应该递归调用它,直到进度==1.0?这是我到目前为止的代码…除了更新/设置进度指示器动画(它显示并显示0%,然后在保存操作完成后消失)外,其他代码都可以正常工作。另外,我在我的CKManager类中使用了一个属性(double progress),我知道这是不正确的,但我不确定还有什么其他方法可以做到这一点。我觉得我在下面的CKManager类中声明/定义的回调方法也不正确。感谢您的指导 CKManager.hIos 如何使用CKModifyRecordsOperation.perRecordProgressBlock更新进度,ios,objective-c,cloudkit,Ios,Objective C,Cloudkit,这与最近的一个线程有关。由于上一个线程(感谢Edwin!),我将cloudkit查询从便利API转换为CKOperations。因此,在使用CKModifyRecordsOperation保存记录时,我可以通过登录perRecordProgressBlock来查看记录的进度,这非常好。但是,我正在尝试将此进度发送回viewcontroller,但我不知道如何执行此操作。我已经为我的所有CloudKit方法创建了一个类-CKManager。我遇到的另一个问题是,我不确定何时在VC中更新进度指示器(
@property (nonatomic, readonly) double progress;
- (void)recordProgressWithCompletionHandler:(void (^)(double progress))completionHandler;
CKM经理
@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];
}
您应该在saveRecord调用中包含进度处理程序,如下所示:
- (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];
}
[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调用的一部分。
我没有测试上面的代码。所以我希望我没有打字错误谢谢!我回家后会试试,让你知道它是怎么工作的。埃德温-它工作得很好!您的saveRecord调用中有一个逗号,但我删除了它是否有效。非常感谢你!
[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];
});
}];