解析取消订阅通道iOS不起作用

时间:2014-04-27 03:02:24

标签: ios parse-platform

我正在尝试从通知渠道取消订阅解析应用,但我的应用却崩溃了。

以下是我的应用代码存在问题:

- (IBAction)genb:(id)sender {
    NSArray *subscribedChannels = [PFInstallation currentInstallation].channels;
    if (![subscribedChannels containsObject:@"gen"]) {
        PFInstallation *currentInstallation = [PFInstallation currentInstallation];
        [currentInstallation addUniqueObject:@"gen" forKey:@"channels"];
        [currentInstallation saveInBackground];
        genl.text = @"Subscribed";
        [gen setTitle:@"Unsubscribe" forState:UIControlStateNormal];
    } else {
        PFInstallation *currentInstallation = [PFInstallation currentInstallation];
        [currentInstallation removeObject:@"gen" forKey:@"channels"];
        [currentInstallation saveInBackground];
        genl.text = @"Not Subscribed";
        [gen setTitle:@"Subscribe" forState:UIControlStateNormal];
    }
}

以下是我的日志所说的内容:

2014-04-26 21:51:28.388 Sulphur Public Schools[5338:3b03] Error: Error Domain=com.parse.networking.error Code=-1011 "Expected status code in (200-299), got 401" UserInfo=0x113ca50d0 {NSLocalizedDescription=Expected status code in (200-299), got 401, NSErrorFailingURLKey=https://api.parse.com/2/create} (Code: 100, Version: 1.2.15)
2014-04-26 21:51:29.268 Sulphur Public Schools[5338:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Operation is invalid after previous operation.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000103445495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001031a499e objc_exception_throw + 43
    2   Sulphur Public Schools              0x00000001000772b9 -[PFRemoveOperation applyToValue:forKey:ofObject:] + 0
    3   Sulphur Public Schools              0x00000001000537d8 -[PFObject(Private) performOperation:forKey:] + 198
4   Sulphur Public Schools              0x0000000100015eb8 -[NotiOptionsTableViewController genb:] + 520
5   UIKit                               0x0000000101d52f06 -[UIApplication sendAction:to:from:forEvent:] + 80
6   UIKit                               0x0000000101d52eb4 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
7   UIKit                               0x0000000101e2f880 -[UIControl _sendActionsForEvents:withEvent:] + 203
8   UIKit                               0x0000000101e2edc0 -[UIControl touchesEnded:withEvent:] + 530
9   UIKit                               0x00000001020766f7 _UIGestureRecognizerUpdate + 5149
10  UIKit                               0x0000000101d89a15 -[UIWindow _sendGesturesForEvent:] + 928
11  UIKit                               0x0000000101d8a6d4 -[UIWindow sendEvent:] + 909
12  UIKit                               0x0000000101d6229a -[UIApplication sendEvent:] + 211
13  UIKit                               0x0000000101d4faed _UIApplicationHandleEventQueue + 9579
14  CoreFoundation                      0x00000001033d4d21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15  CoreFoundation                      0x00000001033d45f2 __CFRunLoopDoSources0 + 242
16  CoreFoundation                      0x00000001033f046f __CFRunLoopRun + 767
17  CoreFoundation                      0x00000001033efd83 CFRunLoopRunSpecific + 467
18  GraphicsServices                    0x00000001045a2f04 GSEventRunModal + 161
19  UIKit                               0x0000000101d51e33 UIApplicationMain + 1010
20  Sulphur Public Schools              0x000000010002ba03 main + 115
21  libdyld.dylib                       0x00000001039ca5fd start + 1
    22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我认为问题与removeOjectLine有关,因为当我尝试按下现在取消订阅按钮时我的应用程序崩溃了。非常感谢任何帮助!

4 个答案:

答案 0 :(得分:4)

据Parse的人说,这个错误:'NSInternalInconsistencyException', reason: 'Operation is invalid after previous operation.'是执行两次原子操作而没有保存的结果。例如,addUniqueObject:forKey:removeObject:forKey都是原子操作。

所以它可能就像你在没有适当保存的情况下在其他方法中运行原子操作一样简单;但是,从个人经验来看,我也注意到,Parse有时神秘地无法保存原子操作的结果,因此,在执行新的原子操作后保存时,应用程序随机崩溃并出现此错误。 / p>

为了避免这个'NSInternalInconsistencyException',我个人尽量避免使用Parse的数组操作。我没有删除一个对象,而是将所有当前对象存储在一个数组中,从该数组中删除该对象,然后将整个修改后的数组发布回数据库。是的,效率低,烦人,但这是我发现的唯一可以防止此错误的方式。

在查看Parse留言板时,似乎很多人在改变'NSInternalInconsistencyException', reason: 'Operation is invalid after previous operation.'时会像在你的问题中那样得到PFInstallation ...例如:PFInstallations gets into an invalid state with "Operation is invalid after previous operation"。希望Parse能尽快解决这个问题。

答案 1 :(得分:1)

虽然看似与错误消息无关,但无效的通道名称可能会影响此错误。

请确认您的频道名称有效。

就我而言,我试图在名为“U.K.”的频道上运行操作,该频道产生了这个错误。

答案 2 :(得分:0)

我只是在违规行之前添加PFInstallation.currentInstallation().saveInBackgroundWithBlock(nil),一切都很好...... 我这样做是因为如果执行点到达那么远,你就会假设之前发生的事情已正确保存,因此重新保存并不危险。

答案 3 :(得分:0)

我遇到了同样的问题,我认为这是因为您正在同时添加和删除频道。我使用saveEventually回调块修复它,看看这段代码:

[currentInstallation
 removeObjectsInArray:toDelete
 forKey:@"channels"];

[currentInstallation saveEventually:^(BOOL succeeded, NSError * _Nullable error) {
    if (succeeded){
        for (NSString *channel in toAdd) {
            if (![[currentInstallation channels]  containsObject:channel]){
                [currentInstallation addUniqueObject:channel forKey:@"channels"];
                [currentInstallation saveEventually];
            }
        }
    }
}];