【问题标题】:Flow of code unexpected意外的代码流
【发布时间】:2014-09-02 22:54:33
【问题描述】:

让我简要解释一下代码。我有一个名为“LinkedParseClass”的类,它包含有关业务的信息。然后我有一个名为'dealParseClass'的类。基本上每个企业最多可以有 3 笔交易。当交易已过期时,我希望删除该交易。所以我从“dealParseClass”中删除了该行/对象。我还在“numberOfActiveDeals”列中保留了 LinkedParseClass 中交易数量的计数。因此,当我删除一笔交易时,我会将计数减 1。

我有以下代码,但它没有按预期流动,并且每次都给我不同的输出。实际上,当我在该特定页面/视图控制器上时,我知道该地点的唯一 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

【问题讨论】:

    标签: ios asynchronous parse-platform flow


    【解决方案1】:

    您的代码的根本问题是您启动了几个异步 任务,然后在下一行代码中假设它们已经完成并且您的数据已经更新。那是错的。例如。当您调用[dealObj deleteInBackground] 时,您不能期望该对象会在下一行中消失。因此方法名称(“在后台删除”)。

    如果你想在异步任务成功后执行一些代码,你可以使用带有完成块的 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!");
     }
    

    【讨论】:

    • 您好 BlackRider,感谢您的帮助。我已经修改了将其放置在 deleteInBackgroundWithBlock 中的代码,请参阅上面的修改后的帖子。但是,流程仍然不正确。我必须在其他地方进行更改吗?
    • 这是一个好的开始。我还建议在完成块中检查调用的结果(因为它可能成功也可能不成功)。看看我如何在上面的代码中使用变量succeeded
    • 酷。流程与我想象的不完全一样,但我已经对其进行了测试,并且似乎每次都有效。谢谢!
    猜你喜欢
    • 2013-03-14
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 2014-09-21
    相关资源
    最近更新 更多