使用where子句从coredata中删除记录 [英] Deleting a record from coredata with a where clause

查看:148
本文介绍了使用where子句从coredata中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体(UserDetails)有两列(用户名,密码)。
for eg:



从这里我填充不同的值,并显示它们一个表视图。

  NSEntityDescription * entity = [NSEntityDescription entityForName:@UserDetails
inManagedObjectContext:managedObjectContext];
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES];
[request setPropertiesToFetch:@ [@username,@password]];
//不知道这将产生与我想象的相同的输出。
self.users = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
[self.tableView reloadData];

希望输出(来自上述代码):




  • (abc,def)


  • (abc,xyz) b $ b

  • (abc123,def123)


  • (s01,s01)



    现在我想向表视图中添加一个删除功能,它应该从表和核心数据记录中删除。




因为它们没有按照与coredata模型相同的顺序填充,所以我想使用类似其中的子句删除它。所以每当我删除(abc,def)它应该删除coredata中的所有类似记录。



有人可以帮助解决这个问题吗?



TIA

解决方案

您可以这样做:

  +(BOOL)deleteDataFromUserDetailsTableWithUserName:(NSString *)strUsername AndPassword:(NSString *)strPassword; 
{

AppDelegate * objApDel =(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [objApDel managedObjectContext];

NSEntityDescription * userEntity = [NSEntityDescription entityForName:kTableName inManagedObjectContext:context];
NSFetchRequest * fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:userEntity];
NSPredicate * p = [NSPredicate predicateWithFormat:@username ==%@ AND password =%@,strUsername,strPassword];
[fetch setPredicate:p];
// ...如果你想要添加排序
NSError * fetchError;
NSArray * fetchedDetails = [context executeFetchRequest:fetch error:& fetchError];
for(self.users * details in fetchedProducts){
[context deleteObject:details];
}
if(fetchError == nil){
return YES;
}
else
{
return NO;
}
}


I have an entity(UserDetails) which has two columns (username,password). for eg:

from this I'm populating distinct values and displaying them on a table view.

     NSEntityDescription *entity = [NSEntityDescription  entityForName:@"UserDetails"
                                                 inManagedObjectContext:managedObjectContext];
         NSFetchRequest *request = [[NSFetchRequest alloc] init];
         [request setEntity:entity];
         [request setResultType:NSDictionaryResultType];
         [request setReturnsDistinctResults:YES]; 
         [request setPropertiesToFetch:@[@"username",@"password"]]; 
//not sure if this will generate the same output as what I'm thinking of.
    self.users = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
         [self.tableView reloadData];

Output expected(from the above code):

  • (abc,def)

  • (abc,xyz)

  • (abc123,def123)

  • (s01,s01)

    Now I want to add a delete feature to the table view which should delete from both table and the core data record.

As they are not populated in the same order as that of the coredata model, I want to delete it using something like a where clause. So whenever I delete (abc,def) it should delete all the similar records in coredata.

can someone help on how to solve this?

TIA

解决方案

You can do like this:

+(BOOL)deleteDataFromUserDetailsTableWithUserName:(NSString*)strUsername AndPassword :(NSString*)strPassword;
{

    AppDelegate *objApDel = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    NSManagedObjectContext *context = [objApDel managedObjectContext];

    NSEntityDescription *userEntity=[NSEntityDescription entityForName:kTableName inManagedObjectContext:context];
    NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
    [fetch setEntity:userEntity];
      NSPredicate *p=[NSPredicate predicateWithFormat:@"username == %@ AND password=%@", strUsername,strPassword];
    [fetch setPredicate:p];
    //... add sorts if you want them
    NSError *fetchError;
    NSArray *fetchedDetails=[context executeFetchRequest:fetch error:&fetchError];
    for (self.users *details in fetchedProducts) {
        [context deleteObject:details];
    }
    if (fetchError == nil) {
        return YES;
    }
    else
    {
        return NO;
    }
}

这篇关于使用where子句从coredata中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆