2014-03-02 111 views
1

我正在使用ReactiveCocoa保持UICollectionView與消息列表保持最新。在我的模型收到新消息後,嘗試更新我的集合視圖時發生崩潰。NSObject(RACKVOWrapper)rac_observeKeyPath:options:observer:block:EXC_BAD_ACCESS Crash

這是我的視圖模型,它通過來自套接字客戶端的委託調用獲取更新。

@interface ConversationViewModel : NSObject 

@property (nonatomic, strong) NSMutableArray *messages; 

- (RACSignal *)rac_signalForMessageReceived; 

@end 

@implementation ConversationViewModel 
.... 
- (void)client:(PSClient *)theClient didReceiveMessage:(PSMessage *)aMessage { 
    [self.messages addObject:aMessage]; 
    [_messageReceivedSubscriber sendNext:nil]; 
} 
.... 
- (RACSignal *)rac_signalForMessageReceived { 
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { 
     self.messageReceivedSubscriber = subscriber; 
     return [RACDisposable disposableWithBlock:^{ 
      self.messageReceivedSubscriber = nil; 
      // Do Nothing 
     }]; 
    }]; 

    [signal setName:@"rac_signalForMessageReceived"]; 

    return signal; 
} 

和消息對象是

@interface Message : NSObject 

@property (nonatomic, assign) NSString *message; 
@property (nonatomic, assign) NSString *name; 

@end 

這裏是控制器如何使用它。

- (void)viewDidLoad 
    [super viewDidLoad]; 

    [self.conversationViewModel.rac_signalForMessageReceived subscribeNext:^(PSMessage *message) { 
     [self.collectionView reloadData]; 
    }]; 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return [self.conversationViewModel.messages count]; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    PSConversationMessageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"conversationCell" forIndexPath:indexPath]; 

    [cell setMessage:self.conversationViewModel.messages[indexPath.row]]; 

    return cell; 
} 

最後,這裏是我如何做單元格內的綁定。

- (void)awakeFromNib { 
    RAC(self.messageLabel, text) = RACObserve(self, message.message); 
    RAC(self.nameLabel, text) = RACObserve(self, message.name); 
} 

只要收集視圖重新加載,我就會在rac_observeKeyPath中得到一個異常:options:observer:block :.在下面的屏幕截圖中可以看到堆棧。 enter image description here

有誰知道爲什麼這會導致異常?或者在ConversationViewModel中處理接收消息的更好方法?

回答

2

我找到了不尋常的崩潰的原因。我的消息對象與使用爲NSString屬性分配的屬性類型一樣。它需要被複制。花了很長時間跟蹤那一個。