2014-09-02 68 views
-2

讓我簡單地解釋代碼。我有一個名爲「LinkedParseClass」的類,它擁有關於業務的信息。然後我有一個叫做'dealParseClass'的類。基本上每個企業最多可以有3個交易。當一筆交易過去了,它的到期日我希望交易被刪除。所以我從'dealParseClass'中刪除那行/對象。我還保留了「numberOfActiveDeals」列中LinkedParseClass中交易數量的計數。所以,當我刪除一筆交易,然後遞減1。代碼流意外

我有以下代碼,但它不按預期流動,每次給我不同的輸出。有效地,當我在那個特定的頁面/視圖控制器上時,我知道該地點的唯一ID。我在'dealParseClass中有一個外鍵。我得到了所有關鍵的交易。然後我試圖遍歷一個數組,從'LinkedParseClass'遞減count(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 

回答

2

與您的代碼的基本問題是你踢掉幾個異步任務,然後,在代碼的最下一行認爲他們已經完成了您的信息已被更新。那是錯的。例如。當您撥打[dealObj deleteInBackground]時,您不能指望該對象在下一行中消失。因此方法名稱(「在後臺刪除」)。

如果要在異步任務成功後執行某些代碼,可以使用帶有完成塊的分析方法,例如,

[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!"); 

或者,您也可以致電解析對象相應同步方法。在這種情況下,您需要注意不要在主隊列中執行此操作,否則當這些調用正在進行時,您的應用將完全無響應:

BOOL result = [dealObj delete]; 
if(result) { 
    NSLog(@"delete success!"); 
} 
+0

嗨BlackRider,感謝您的幫助。我已經修改了將它放在deleteInBackgroundWithBlock中的代碼,請參閱上面修改的帖子。但流量仍然不正確。我必須在其他地方進行更改嗎? – Anthony 2014-09-03 07:30:52

+0

這是一個好的開始。我還建議在完成塊中檢查調用的結果(因爲它可能成功也可能不成功)。看看我如何在我的代碼中使用變量'succeeded'來縮小上面的代碼。 – Macondo2Seattle 2014-09-03 16:29:39

+0

很酷。流程並不完全如我所想象的那樣,但我已經測試過,似乎每次都有效。謝謝! – Anthony 2014-09-03 18:25:07