在 Parse 中调用 saveInBackgroundWithBLock 后,我可以在循环中删除数组的内容吗
can I delete the content of the array im loping after calling saveInBackgroundWithBLock in Parse
我有一个应用程序可以保存 public url,然后在单击 a 按钮时将它们上传到 syns 以解析对它们的引用。在我调用解析之后,在块中,我想重置我正在使用的数组,但我有点不清楚删除引用是否会产生某种空指针错误。这是我的代码
for (NSString *theString in sharedDataController.filesToUpload) {
// Create PFObject with recipe information
PFObject *parseImage = [PFObject objectWithClassName:@"Photos"];
[parseImage setObject:theString forKey:@"myURL"];
[parseImage setObject:object forKey:@"photoUser"];
[parseImage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// Show success message
NSLog(@"Parse Image Saved");
} else {
NSLog(@"Parse Image Error");
}
}];
// if I delete the contents sharedDAtaController.filesToUpload here, will that create an issue
}
是的,这会产生问题。对 saveInBackgroundWithBlock
的调用未在完成块之外完成。您将需要一些方法来合并所有调用,并且仅在最后一个完成块触发后才删除 sharedDAtaController.filesToUpload
。
你可以创建一个atomic属性"pendingSaves",每次调用saveInBackgroundWithBlock
之前增加它,在完成块中减少它, 并在 saveInBackgroundWithBlock
的完成块中删除 sharedDAtaController.filesToUpload
仅当 pendingSaves == 0
您正在从数组中创建对象并一一上传所有对象。但是 Parse 也为您提供批量上传数组。所以创建 PFObjects 数组并将其上传到 Parse
NSMutableArray *arrParseObjects = [[NSMutableArray alloc]init];
for (NSString *theString in sharedDataController.filesToUpload) {
// Create PFObject with recipe information
PFObject *parseImage = [PFObject objectWithClassName:@"Photos"];
[parseImage setObject:theString forKey:@"myURL"];
[parseImage setObject:object forKey:@"photoUser"];
[arrParseObjects addObject:parseImage];
// if I delete the contents sharedDAtaController.filesToUpload here, will that create an issue
}
[PFObject saveAllInBackground:arrParseObjects block:^(BOOL succeeded, NSError *PF_NULLABLE_S error){
if (succeeded) {
// You just saved all objects in Parse
}else{
}
}];
如果我们谈论您在删除对象时发生的崩溃,则结束。在循环遍历数组时,我们永远不能从数组中删除任何对象。 1 解决方案是我们需要对其使用 反向枚举。
[sharedDAtaController.filesToUpload enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// Remove the object after work
// Make sure your array is a NSMutableArray.
}];
我有一个应用程序可以保存 public url,然后在单击 a 按钮时将它们上传到 syns 以解析对它们的引用。在我调用解析之后,在块中,我想重置我正在使用的数组,但我有点不清楚删除引用是否会产生某种空指针错误。这是我的代码
for (NSString *theString in sharedDataController.filesToUpload) {
// Create PFObject with recipe information
PFObject *parseImage = [PFObject objectWithClassName:@"Photos"];
[parseImage setObject:theString forKey:@"myURL"];
[parseImage setObject:object forKey:@"photoUser"];
[parseImage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// Show success message
NSLog(@"Parse Image Saved");
} else {
NSLog(@"Parse Image Error");
}
}];
// if I delete the contents sharedDAtaController.filesToUpload here, will that create an issue
}
是的,这会产生问题。对 saveInBackgroundWithBlock
的调用未在完成块之外完成。您将需要一些方法来合并所有调用,并且仅在最后一个完成块触发后才删除 sharedDAtaController.filesToUpload
。
你可以创建一个atomic属性"pendingSaves",每次调用saveInBackgroundWithBlock
之前增加它,在完成块中减少它, 并在 saveInBackgroundWithBlock
的完成块中删除 sharedDAtaController.filesToUpload
仅当 pendingSaves == 0
您正在从数组中创建对象并一一上传所有对象。但是 Parse 也为您提供批量上传数组。所以创建 PFObjects 数组并将其上传到 Parse
NSMutableArray *arrParseObjects = [[NSMutableArray alloc]init];
for (NSString *theString in sharedDataController.filesToUpload) {
// Create PFObject with recipe information
PFObject *parseImage = [PFObject objectWithClassName:@"Photos"];
[parseImage setObject:theString forKey:@"myURL"];
[parseImage setObject:object forKey:@"photoUser"];
[arrParseObjects addObject:parseImage];
// if I delete the contents sharedDAtaController.filesToUpload here, will that create an issue
}
[PFObject saveAllInBackground:arrParseObjects block:^(BOOL succeeded, NSError *PF_NULLABLE_S error){
if (succeeded) {
// You just saved all objects in Parse
}else{
}
}];
如果我们谈论您在删除对象时发生的崩溃,则结束。在循环遍历数组时,我们永远不能从数组中删除任何对象。 1 解决方案是我们需要对其使用 反向枚举。
[sharedDAtaController.filesToUpload enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// Remove the object after work
// Make sure your array is a NSMutableArray.
}];