我是自己创建的数据库保存在本地
//任何消息都会先经过这个方法
- (void)onRCIMReceiveMessage:(RCMessage *)message left:(int)left {
if ([message.objectName isEqualToString:@"RC:CmdMsg"]) {
RCCommandMessage *commandMessage = (RCCommandMessage *)message.content;
NSData *jsonData = [commandMessage.data dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
NSMutableDictionary *dataDictionary = [NSMutableDictionary dictionaryWithDictionary:dict];
[dataDictionary setObject:message.content.senderUserInfo.userId forKey:@"userId"];
[dataDictionary setObject:message.content.senderUserInfo.name forKey:@"name"];
[dataDictionary setObject:message.content.senderUserInfo.portraitUri forKey:@"portraitUri"];
//视频通话
if ([dict[@"messageType"] isEqualToString:MESSAGETYPE_VIDEOCALL]) {
//视频语音通话 消息记录不需要保存在本地
[UserInfo sharedUserInfo].msgType = 3;
[UserInfo sharedUserInfo].vedioUrl = [dataDictionary objectForKey:@"fromVideoUrl"];
dispatch_async(dispatch_get_main_queue(), ^{
[self showVideoView:dataDictionary withString:@"邀请你视频聊天"];
});
}
//语音通话
if ([dict[@"messageType"] isEqualToString:MESSAGETYPE_VOICECALLS]) {
dispatch_async(dispatch_get_main_queue(), ^{
if([self.window viewWithTag:5555] || [self.window viewWithTag:5556])return;
if([UserInfo sharedUserInfo].yujianta == 34){
[UserInfo sharedUserInfo].msgType = 4;
[UserInfo sharedUserInfo].vedioUrl = @"";
[self showVideoView:dataDictionary withString:@"邀请你语音聊天"];
}
});
}
} else {
//更新未读数
dispatch_async(dispatch_get_main_queue(), ^{
[self setupUnreadMessageCount];
});
//设置顶置
if ([message.content.senderUserInfo.name isEqualToString:@"最新访客"] || [message.content.senderUserInfo.name isEqualToString:@"系统消息"] || [message.content.senderUserInfo.name isEqualToString:@"客服消息"]) {
[[RCIMClient sharedRCIMClient] setConversationToTop:message.conversationType targetId:message.targetId isTop:YES];
}
//将消息发送者用户信息保存到本地数据库
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"RYUserInfo"];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"userId = %@", message.content.senderUserInfo.userId];
request.predicate = pre;
NSArray *resArray = [_context executeFetchRequest:request error:nil];
//如果不存在该用户信息就保存
if (resArray.count == 0) {
//插入数据
RYUserInfo * info = [NSEntityDescription insertNewObjectForEntityForName:@"RYUserInfo" inManagedObjectContext:_context];
info.userId = message.content.senderUserInfo.userId;
info.name = message.content.senderUserInfo.name;
info.portraitUri = message.content.senderUserInfo.portraitUri;
NSError *error = nil;
if ([_context save:&error]) {
NSLog(@"数据插入到数据库成功");
}else{
NSLog(@"数据插入到数据库失败");
}
} else {
//如果存在 就查看传来的数据是否和本地一致不同就修改
RYUserInfo *info = resArray[0];
if (![info.name isEqualToString:message.content.senderUserInfo.name]) info.name = message.content.senderUserInfo.name;
if (![info.portraitUri isEqualToString:message.content.senderUserInfo.portraitUri]) info.portraitUri = message.content.senderUserInfo.portraitUri;
NSError *error = nil;
if ([_context save:&error]) {
NSLog(@"修改成功");
}else{
NSLog(@"更新数据失败, %@", error);
}
}
}
}
/设置消息列表头像昵称
- (void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *userInfo))completion {
if ([userId isEqualToString:[RCIM sharedRCIM].currentUserInfo.userId]) {
return completion([[RCUserInfo alloc] initWithUserId:userId name:[RCIM sharedRCIM].currentUserInfo.name portrait:[RCIM sharedRCIM].currentUserInfo.portraitUri]);
} else {
//查询数据库是否存在 UserID
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"RYUserInfo"];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"userId = %@", userId];
request.predicate = pre;
NSArray *resArray = [_context executeFetchRequest:request error:nil];
//存在
if (resArray.count != 0) {
RYUserInfo *userInfo = resArray[0];
RCUserInfo *user = [[RCUserInfo alloc] initWithUserId:userInfo.userId name:userInfo.name portrait:userInfo.portraitUri];
return completion(user);
}
}
}
网友评论