Last active
August 29, 2015 14:15
-
-
Save dodikk/f1ad75f1522b4979fcb4 to your computer and use it in GitHub Desktop.
Import protocol to coredata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)importMessageFromWebSocket:(id<HJMessage>)singleMessage | |
{ | |
NSPredicate* findExistingMessage = | |
[NSPredicate predicateWithFormat: @"(identifier == %@) && (conversation.identifier == %@)", | |
singleMessage.messageId, | |
singleMessage.roomId]; | |
NSManagedObjectContext* context = [NSManagedObjectContext MR_contextForCurrentThread]; | |
HLJMessage* messageToImport = nil; | |
HLJMessage* existingMessage = [HLJMessage MR_findFirstWithPredicate: findExistingMessage | |
inContext: context]; | |
if (nil != existingMessage) | |
{ | |
messageToImport = existingMessage; | |
} | |
else | |
{ | |
messageToImport = [HLJMessage MR_createEntityInContext: context]; | |
HLJConversation* conversation = [HLJConversation MR_findFirstByAttribute: @"identifier" | |
withValue: self.conversation.identifier | |
inContext: context]; | |
[conversation addMessages: messageToImport]; | |
} | |
// Update fields | |
{ | |
messageToImport.identifier = singleMessage.messageId; | |
messageToImport.text = singleMessage.messageText; | |
messageToImport.timestamp = singleMessage.timestamp; | |
messageToImport.userName = singleMessage.userId; | |
messageToImport.read = singleMessage.isAlreadyRead; | |
} | |
[context MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) | |
{ | |
NSLog(@"websocket message saved"); | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another take with dedicated queue and context
Initialization