解析 saveInBackgroundWithBlock
parse saveInBackgroundWithBlock
我是 objective c 的新手。
当我按下保存按钮时,我试图设置保存按钮无法使用。并同时执行 saveInBackgroundWithBlock。
*** 由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因:“-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] 只能从主线程调用。”
这是我的代码
`
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];
}];
`
谢谢你的帮助
您收到此错误的原因是您试图从后台线程影响 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];
});
这应该适合你。
我是 objective c 的新手。 当我按下保存按钮时,我试图设置保存按钮无法使用。并同时执行 saveInBackgroundWithBlock。
*** 由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因:“-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] 只能从主线程调用。”
这是我的代码 `
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];
}];
`
谢谢你的帮助
您收到此错误的原因是您试图从后台线程影响 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];
});
这应该适合你。