1

我有一些可以喜欢的故事元素的提要,就像 Facebook 一样。

我的第一步只是使用故事模型对象来获得喜欢的状态并在 CKButtonComponent 中显示它。该解决方案的问题在于该按钮不能足够快地代表预期的新状态。相反,我正在进行网络服务调用,该响应改变了故事,然后按钮被改变。

为了获得更快的按钮状态更改,我为该 CKComponent 构建了一个 CKComponentController ,它会在按下按钮时更新组件的状态:

- (void)toggleLike {
    [self.component updateState:^(id oldState){
        if ([oldState integerValue] == LikeComponentStateInitial || [oldState integerValue] == LikeComponentStateNotLiked) {
            return @(LikeComponentStateLiked);
        }
        return @(STGLikeComponentStateNotLiked);
    }];
    [WebService toggleLikeForStory:...];
}

此外,我正在拨打网络服务电话。该解决方案的问题在于,当我以足够快的速度多次按下like-Button时,应用程序崩溃并显示以下消息:

** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“未处理的组件操作 toggleLike 跟随响应者链 nil”

The crash happens at an Assert in void CKComponentActionSend(CKComponentAction action, CKComponent *sender, id context, CKComponentActionSendBehavior behavior) in the CKComponentAction class. I don't know why this happens only when i hit the button fast enough multiple times. It seems to have something to do with reload of all components which happens when the new story data get saved from the web service response. The reload of all components and at the same time the CKButtonComponent getting the touch event seems to lead to the crash.

The CKComponentAction of the CKButtonComponent is the described CKComponentController method toggleLike.

So what would be a good solution in this case? Is there a way to do this without changing the story model object?

I have also the feeling that my approach to use the state to override some model data (like status) is generally not really a good idea.

4

0 回答 0