CoreData中的数据处理的一些问题 增删改查
array 0 = ( ? ? "<MsgForPush: 0x1d5c4260> (entity: MsgForPush; id: 0x1d5b0720 <x-coredata://A174ED32-E568-4FFE-AC00-CFD985E7D986/MsgForPush/p1> ; data: <fault>)", ? ? "<MsgForPush: 0x1d59a450> (entity: MsgForPush; id: 0x1d598eb0 <x-coredata://A174ED32-E568-4FFE-AC00-CFD985E7D986/MsgForPush/p2> ; data: <fault>)", ? ? "<MsgForPush: 0x1d59a500> (entity: MsgForPush; id: 0x1d5a04a0 <x-coredata://A174ED32-E568-4FFE-AC00-CFD985E7D986/MsgForPush/p3> ; data: <fault>)" ) ? 直接取值 data:<fault> ? 应该使用valueForkey来获取数据? 从数据库中删除数据
+ (void)deleteMsgInfo { ? ? NSManagedObjectContext *objectContext = [[AppDelegateAppDelegate] managedObjectContext]; ?? ? NSEntityDescription *entity = [NSEntityDescriptionentityForName:@"MsgForPush"inManagedObjectContext:objectContext]; ? ? //查询数据库,搜索出该类 ? ?NSFetchRequest *fetchRequest = [[NSFetchRequestalloc] init]; ? ? [fetchRequestsetEntity:entity]; ? ?NSError *error; ? ?NSArray *objects = [objectContext executeFetchRequest:fetchRequest error:&error]; ? ?for (MsgForPush *searchin objects) {//清空该类数据 ? ? ? ?NSString* alert_title = [search valueForKey:@"alert"]; ? ? ? ?NSLog(@"delete title = %@",alert_title); ? ? ? ? [objectContextdeleteObject:search]; ? ? } ? ?if ([objectContext hasChanges]) ? ? { ? ? ? ?if (![objectContext save:&error]) ? ? ? ? { ? ? ? ? ? ? NSLog(@"There was an error"); ? ? ? ? } ? ? } }
@月城眠
?生日快乐 // 20130618115800
+(NSArray*)searchInTable:(NSString*)table ofAttribute:(NSString*)attr valued:(id)val inContext:(NSManagedObjectContext*)context//查找表table中属性attr的值为val的数据 { ? ? NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:table]; ? ??NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K == %@)",attr,val]; ? ? [fetchRequest setPredicate:predicate]; ? ? NSError* error; ? ? NSArray* result = [context executeFetchRequest:fetchRequest error:&error]; ? ? if (error) { ? ? ? ? NSString* info = [NSString stringWithFormat:@"查询表%@中属性%@值为%@时出现错误==%@",table,val,error.description]; ? ? ? ? NSLog(@"%@",info); ? ? } ? ? return result; } //更新:直接修改对象,保存managedObjectContext就好 + (BOOL)updateStatus:(NSString*)alert{ ? ? NSManagedObjectContext* context = [AppDelegate AppDelegate].managedObjectContext; ? ? NSArray* read = [self searchInTable:@"MsgForPush" ofAttribute:dDBK_alert valued:alert inContext:context]; ? ? NSLog(@"alert == %@",alert); ? ? NSLog(@"----read = %@",read); ? ? [read setValue:@"1" forKey:dDBK_status]; ? ? NSError *error = nil; ? ? if (![context save:&error]) { ? ? ? ? NSLog(@"Unresolved error %@,%@",error,[error userInfo]); ? ? ? ? abort(); ? ? ? ? return NO; ? ? } ?? ? ? ? NSLog(@"update success"); ? ? return YES; } ////////////////////// 查重的问题~~~~~ ? for (int i=0; i<array.count; i++) ? ? { ? ? ? ? //查重 ? ? ? ? for (int j=i+1; j<array.count; j++) ? ? ? ? { ? ? ? ? ? ? NSDictionary *inquiryDic = [array objectAtIndex:i]; ? ? ? ? ? ? NSDictionary* dic2 = [array objectAtIndex:j]; ? ? ? ? ? ? if ([[inquiryDic objectForKey:@"alert"] isEqualToString:[dic2 valueForKey:@"alert"]]) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? [array removeObjectAtIndex:j]; ? ? ? ? ? ? ? ? [objectContext delete:[array objectAtIndex:j]]; ? ? ? ? ? ? ? ? j--; ? ? ? ? ? ? } ? ? ? ? } ? ? } 其它问题 更新中。。。。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |