Let me explain the code briefly. I have a class called 'LinkedParseClass', this holds information on a business. Then I have a class called 'dealParseClass'. Essentially each business can have 3 deals maximum. When a deal is past its expiry date I want the deal to be deleted. So I delete that row/object from the 'dealParseClass'. I also retain the count of the number of deals in the LinkedParseClass in the column 'numberOfActiveDeals'. So when I delete a deal I then decrement the count by 1.
I have the following code but it does not flow as expected and gives me different output each time. Effectively when I am on that particular page/viewcontroller I know the unique id of the place. I have a foreign key in 'dealParseClass. I get all of the deals with that key. Then I am trying to iterate over an array, decrementing the count (numberOfActiveDeals) from 'LinkedParseClass' and then deleting the object from the 'dealParseClass'.
When a deal is in the past I effectively check this and if it is I look to carry out the above (decrement and delete object). However the flow of my code is unexpected. I am not too sure why this is happening. Any suggestions/help would be greatly appreciated.
//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
The fundamental problem with your code is you kick off several asynchronous tasks, and then, in the very next line of code assume that they have already completed and your data has been updated. That is wrong. E.g. when you call [dealObj deleteInBackground]
, you can't expect the object to be gone in the next line. Hence the method name ("delete in background").
If you want to execute some code after the async task succeeds, you may use the Parse methods with completion blocks, e.g.
[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!");
Alternatively, you could call the corresponding synchronous methods on Parse objects. In this case you need to be careful not to do it in the main queue, otherwise your app will be completely unresponsive while those calls are in progress:
BOOL result = [dealObj delete];
if(result) {
NSLog(@"delete success!");
}