这与最近的线程Update progress with MRProgress有关。由于前面的线程,我将cloudkit查询从方便API转换为CKOperations (谢谢Edwin!)。因此,在使用CKModifyRecordsOperation保存记录时,我可以通过perRecordProgressBlock中的日志记录来查看记录的进度,这是很棒的。但是,我试图将这个进度发送回视图控制器,但我不知道如何做到这一点。我已经为我的所有CloudKit方法- CKManager创建了一个类。我遇到的另一个问题是,我不确定何时更新VC中的进度指示符(使用MRProgress框架)。我是在CKManager中调用保存操作之前、期间还是之后调用它?应该在进度== 1.0之前递归地调用它吗?下面是我编写的代码,因此far...every工作正常,除了更新/动画进度指示器(它会显示0%,然后在保存操作完成后消失)。另外,我在我的CKManager类中使用了一个属性(双进度),我知道这是不正确的,但是我不知道如何做。我也不认为我在下面的CKManager类中声明/定义的回调方法也是正确的。如有任何指导,将不胜感激!
CKManager.h
@property (nonatomic, readonly) double progress;
- (void)recordProgressWithCompletionHandler:(void (^)(double progress))completionHandler;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];
}这是一种方法,从相机拍摄照片并调用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];
});
}];发布于 2015-04-06 17:12:56
您应该将进度处理程序包括在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调用的一部分。上面的代码没有经过我的测试。所以我希望我没有打错字
https://stackoverflow.com/questions/29473336
复制相似问题