CoreData增删改查
messageFetchRequest.predicate = [NSPredicate predicateWithFormat:@"toAccount == '130000011111'"];
1.增加一条数据
NSEntityDescription *enity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
Contact *oneContact = [[Contact alloc]initWithEntity:enity insertIntoManagedObjectContext:self.managedObjectContext];
oneContact.iid = [NSString stringWithFormat:@"%lu", (unsigned long)containsContact.count];
oneContact.account = realUserName;
oneContact.status = [[item attributeForName:@"subscription"]stringValue];
[self.managedObjectContext save:nil];
2.删除一条数据
//不推荐使用谓词,有的时候并不能精确度的找到
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Contact"];
// request.predicate = [NSPredicate predicateWithFormat:@"account = '%@'",shieldUsername];
NSArray *datas = [self.managedObjectContext executeFetchRequest:request error:nil];
for (Contact *model in datas) {
if ([model.account isEqualToString:shieldUsername]) {
[self.managedObjectContext deleteObject:model];
NSLog(@"删除成功");
}
}
if ([self.managedObjectContext save:nil]) {
NSLog(@"保存成功");
}
else
{
NSLog(@"删除失败");
}
3.修改一条数据,先查后改
UITextField *textF = alert.textFields.firstObject;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Contact"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"account == %@",self.contactModel.account];
NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil]; //_managedObjectContext
for (Contact *model in result) {
model.name = textF.text;
_contactModel = model;
[self.tableView reloadData];
}
[_managedObjectContext save:nil];
或
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"RecentSession"];
request.predicate = [NSPredicate predicateWithFormat:@"fromNumber == %@",weakSelf.contactModel.account];
NSArray *arr = [weakSelf.managedObjectContext executeFetchRequest:request error:nil];
RecentSession *recentModel = arr.firstObject;
if (arr.count != 0) {
recentModel.fromName = textF.text;
}
[weakSelf.managedObjectContext save:nil];
4.全表查询
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Contact"];
NSArray *containsContact = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
for (Contact *obj in containsContact) {
if ([obj.account isEqualToString:realUserName]) {
return;
}
5.条件查询
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Pre_Message"];
request.predicate = [NSPredicate predicateWithFormat:@"other_msg_id == %@",msgID]; //注意此处@"other_msg_id == %@"
NSArray *arr = [self.managedobjectContext executeFetchRequest:request error:nil];
6.谓词的使用
-(void)sendFailMessage
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Pre_Message"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"msgFrom == 'me' and myAccount == %@ and msgType = '0' and msgStatus == 'f' or msgStatus == %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userPhone"],@""];
NSArray *arr = [self.objectContext executeFetchRequest:fetchRequest error:nil];
if (arr.count > 0) {
NSArray *orderArray = [self orderedTimeWithArray:arr];
for (Pre_Message *model in orderArray) {
[[XmppMessageSenderHelper shareSendMessageHelper] sendMessageToJid:[NSString stringWithFormat:@"%@@casing.com",model.toAccount] type:model.msgType context:model.msg1 msgId:model.iid sizelong:nil];
if ([orderArray indexOfObject:model] >= 100) {
break;
}
}
}
}