让我简要解释一下代码。我有一个名为' LinkedParseClass'的类,它包含有关业务的信息。然后我有一个名为' dealParseClass'的课程。基本上每个企业最多可以有3笔交易。当交易超过它的到期日时,我希望删除该交易。所以我从' dealParseClass'中删除了该行/对象。我还保留了Column' numberOfActiveDeals'中LinkedParseClass中交易数量的计数。因此,当我删除交易时,我将计数减少1.
我有以下代码,但它没有按预期流动,每次都给我不同的输出。当我在那个特定页面/ viewcontroller上时,我知道这个地方的唯一ID。我在'dealParseClass中有一个外键。我得到了这个钥匙的所有交易。然后我试图迭代一个数组,从' LinkedParseClass'中减少计数(numberOfActiveDeals)。然后从' dealParseClass'中删除该对象。
当交易过去时,我会有效地检查这个,如果是,我会执行上述(减量和删除对象)。但是我的代码流是出乎意料的。我不太清楚为什么会这样。任何建议/帮助将不胜感激。
//Searching over deal object with the objectID found in place object
PFQuery *dealParseQuery = [PFQuery queryWithClassName:@"dealParseClass"];
[dealParseQuery whereKey:@"dealPlaceObjectID" equalTo:self.previewDealModel.placeObjectId];//checks foreign key
[dealParseQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(@"ViewWillAppear Array of deal objects: %@", objects);
if (![objects count] == 0) {//checks to ensure the place has deals
for (PFObject *dealObj in objects) {
NSLog(@"You have just entered for");
NSDate *expiryDateForValidation = dealObj[@"dealExpiryDate"];
NSLog(@"Expiry date for validation is: %@", expiryDateForValidation);
if ([expiryDateForValidation timeIntervalSinceNow] > -61.0) {
[dealObj deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
NSLog(@"You have just entered IF");
PFQuery *linkedBusinessParseQuery = [PFQuery queryWithClassName:@"linkedBusinessParseClass"];
[linkedBusinessParseQuery getObjectInBackgroundWithId:self.previewDealModel.placeObjectId block:^(PFObject *linkedBizobject, NSError *error) {
NSLog(@"Just entered linkedbusinessParseQuery");
//Get original count and log it. Here for logging purposes
int dealCountUserDelete = [[linkedBizobject objectForKey:@"numberOfDealsActive"]intValue];
NSLog(@"Original deal count: %i", dealCountUserDelete);
//Crucial part of the code that decrements by one
NSNumber *decrementNumber = [NSNumber numberWithInt:-1];
[linkedBizobject incrementKey:@"numberOfDealsActive" byAmount:decrementNumber];
//Get new count and log it. Here for logging purposes
int dealCountAfter = [[linkedBizobject objectForKey:@"numberOfDealsActive"]intValue];
NSLog(@"Deal count after: %i", dealCountAfter);
//You update the object in the decrement. Now you must save it back to Parse
[linkedBizobject saveInBackground];
//log deal object to be deleted
NSLog(@"deal object to be deleted: %@", dealObj);
NSLog(@"deal object has now been deleted: %@", dealObj);
//resetting dealcounts
dealCountUserDelete = 0;
dealCountAfter = 0;
NSLog(@"Reset-: dealCountUserDelete is: %d -- dealCountaAfter is: %d", dealCountUserDelete, dealCountAfter);
NSLog(@"Just left linkedbusinessParseQuery");
}];
}];
NSLog(@"delete in background complete");
NSLog(@"You have just left IF");
}
NSLog(@"You have just left for");
}
}
}];
2014-09-03 08:20:52.579 CouponLocation[4585:60b] ViewWillAppear Array of deal objects: (
"<dealParseClass:QwrgBFnZCK:(null)> {\n dealDescription = \"Deal chelsea description\";\n dealExpiryDate = \"2014-09-02 15:48:00 +0000\";\n dealFinalPrice = 0;\n dealImage = \"<PFFile: 0x1158985a0>\";\n dealOriginalPrice = 85;\n dealPercentageOff = 0;\n dealPlaceObjectID = DYxAju6pXR;\n dealStartDate = \"2014-09-02 19:49:49 +0000\";\n dealStatus = Active;\n dealTitle = \"deal chelse\";\n dealType = \"Buy One Get One Free\";\n}",
"<dealParseClass:zthLtvwUop:(null)> {\n dealDescription = \"Xxx desc\";\n dealExpiryDate = \"2014-09-10 07:15:36 +0000\";\n dealFinalPrice = 0;\n dealImage = \"<PFFile: 0x115897ec0>\";\n dealOriginalPrice = 88;\n dealPercentageOff = 0;\n dealPlaceObjectID = DYxAju6pXR;\n dealStartDate = \"2014-09-03 07:15:43 +0000\";\n dealStatus = Active;\n dealTitle = xxx;\n dealType = \"Buy One Get One Half Price\";\n}",
"<dealParseClass:Yl5AtXfkgZ:(null)> {\n dealDescription = \"Ttt desc\";\n dealExpiryDate = \"2014-09-02 11:01:00 +0000\";\n dealFinalPrice = 0;\n dealImage = \"<PFFile: 0x1158895e0>\";\n dealOriginalPrice = 888;\n dealPercentageOff = 0;\n dealPlaceObjectID = DYxAju6pXR;\n dealStartDate = \"2014-09-02 22:01:39 +0000\";\n dealStatus = Active;\n dealTitle = ttt;\n dealType = \"Buy One Get One Free\";\n}"
)
2014-09-03 08:20:52.579 CouponLocation[4585:60b] You have just entered for
2014-09-03 08:20:52.579 CouponLocation[4585:60b] Expiry date for validation is: 2014-09-02 15:48:00 +0000
2014-09-03 08:20:52.580 CouponLocation[4585:60b] You have just left for
2014-09-03 08:20:52.580 CouponLocation[4585:60b] You have just entered for
2014-09-03 08:20:52.580 CouponLocation[4585:60b] Expiry date for validation is: 2014-09-10 07:15:36 +0000
2014-09-03 08:20:52.580 CouponLocation[4585:60b] delete in background complete
2014-09-03 08:20:52.581 CouponLocation[4585:60b] You have just left IF
2014-09-03 08:20:52.581 CouponLocation[4585:60b] You have just left for
2014-09-03 08:20:52.581 CouponLocation[4585:60b] You have just entered for
2014-09-03 08:20:52.581 CouponLocation[4585:60b] Expiry date for validation is: 2014-09-02 11:01:00 +0000
2014-09-03 08:20:52.581 CouponLocation[4585:60b] You have just left for
2014-09-03 08:20:53.483 CouponLocation[4585:60b] You have just entered IF
2014-09-03 08:20:53.713 CouponLocation[4585:60b] Just entered linkedbusinessParseQuery
2014-09-03 08:20:53.714 CouponLocation[4585:60b] Original deal count: 3
2014-09-03 08:20:53.715 CouponLocation[4585:60b] Deal count after: 2
2014-09-03 08:20:53.715 CouponLocation[4585:60b] deal object to be deleted: <dealParseClass:zthLtvwUop:(null)> {
dealDescription = "Xxx desc";
dealExpiryDate = "2014-09-10 07:15:36 +0000";
dealFinalPrice = 0;
dealImage = "<PFFile: 0x115897ec0>";
dealOriginalPrice = 88;
dealPercentageOff = 0;
dealPlaceObjectID = DYxAju6pXR;
dealStartDate = "2014-09-03 07:15:43 +0000";
dealStatus = Active;
dealTitle = xxx;
dealType = "Buy One Get One Half Price";
}
2014-09-03 08:20:53.716 CouponLocation[4585:60b] deal object has now been deleted: <dealParseClass:zthLtvwUop:(null)> {
dealDescription = "Xxx desc";
dealExpiryDate = "2014-09-10 07:15:36 +0000";
dealFinalPrice = 0;
dealImage = "<PFFile: 0x115897ec0>";
dealOriginalPrice = 88;
dealPercentageOff = 0;
dealPlaceObjectID = DYxAju6pXR;
dealStartDate = "2014-09-03 07:15:43 +0000";
dealStatus = Active;
dealTitle = xxx;
dealType = "Buy One Get One Half Price";
}
2014-09-03 08:20:53.716 CouponLocation[4585:60b] Reset-: dealCountUserDelete is: 0 -- dealCountaAfter is: 0
2014-09-03 08:20:53.716 CouponLocation[4585:60b] Just left linkedbusinessParseQuery
答案 0 :(得分:2)
您的代码的基本问题是您启动了几个异步任务,然后在下一行代码中假设它们已经完成并且您的数据已经更新。那是错的。例如。当你致电[dealObj deleteInBackground]
时,你不能指望对象在下一行中消失。因此方法名称(&#34;在后台删除&#34;)。
如果要在异步任务成功后执行某些代码,可以将Parse方法与完成块一起使用,例如:
[dealObj deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if(succeeded) {
NSLog(@"delete success!");
// now you know the object has been deleted
}
}];
NSLog(@"dealObj may or may not still exist at this point!");
或者,您可以在Parse对象上调用相应的同步方法。在这种情况下,您需要注意不要在主队列中执行此操作,否则当这些调用正在进行时,您的应用将完全无响应:
BOOL result = [dealObj delete];
if(result) {
NSLog(@"delete success!");
}