Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用where子句从coredata中删除记录_Ios_Objective C_Core Data - Fatal编程技术网

Ios 使用where子句从coredata中删除记录

Ios 使用where子句从coredata中删除记录,ios,objective-c,core-data,Ios,Objective C,Core Data,我有一个实体(UserDetails),它有两列(用户名、密码)。 例如: 由此,我填充不同的值并将它们显示在表视图中 NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserDetails" inManagedObjectContext:managedObjectContext]

我有一个实体(UserDetails),它有两列(用户名、密码)。 例如:

由此,我填充不同的值并将它们显示在表视图中

     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];
预期输出(来自上述代码):

  • (abc,def)

  • (abc,xyz)

  • (abc123,def123)

  • (s01,s01)

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

由于它们的填充顺序与coredata模型的填充顺序不同,因此我想使用类似于
where
子句的方法删除它。因此,每当我删除(abc,def)时,它都应该删除coredata中的所有类似记录

有人能帮忙解决这个问题吗


具有iOS 9的TIA

可用。您可以使用它从CoreData中删除对象,类似于使用
WHERE
子句在
SQL
中删除对象

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"UserDetails"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username==%@ AND password=%@", @"abc", @"def"];
[request setPredicate:predicate];

NSBatchDeleteRequest *deleteRequest = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request];
NSError *error = nil;
[myPersistentStoreCoordinator deleteRequest withContext:myContext error:&error];
对于以前的版本,您必须先获取所有对象,然后分别删除它们。没有别的办法


此外,如果您使用的是
NSFetchedResultsController
,则删除这些对象时会收到通知,从而将它们从
UITableView
中删除

支持iOS 9
NSBatchDeleteRequest
。您可以使用它从CoreData中删除对象,类似于使用
WHERE
子句在
SQL
中删除对象

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"UserDetails"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username==%@ AND password=%@", @"abc", @"def"];
[request setPredicate:predicate];

NSBatchDeleteRequest *deleteRequest = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request];
NSError *error = nil;
[myPersistentStoreCoordinator deleteRequest withContext:myContext error:&error];
对于以前的版本,您必须先获取所有对象,然后分别删除它们。没有别的办法

此外,如果您使用的是
NSFetchedResultsController
,则删除这些对象时会收到通知,从而将它们从
UITableView
中删除

您可以这样做:

+(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;
    }
}
您可以这样做:

+(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;
    }
}

您可以从表视图数据源(我相信是
self.users
)中找到要删除的对象,然后从
managedObjectContext
@EricQian中删除它们,所以我需要多次这样做?从表数据源中删除一次,然后从managedObjectContext中删除?如果不在UI上显示,为什么会有重复项?@Wain每次用户登录系统时,我只是存储用户名和密码。在Android中实现了同样的功能(相对来说比较容易),但是面对客观ci的问题,你必须想知道为什么要在核心数据中存储用户名和密码?!?您可以从表视图数据源(我相信是
self.users
)中找到要删除的对象,然后从
managedObjectContext
@EricQian中删除它们,所以我需要多次这样做?从表数据源中删除一次,然后从managedObjectContext中删除?如果不在UI上显示,为什么会有重复项?@Wain每次用户登录系统时,我只是存储用户名和密码。在Android中实现了同样的功能(相对来说比较容易),但是面对客观ci的问题,你必须想知道为什么要在核心数据中存储用户名和密码?!?谢谢你,安科。我会试试看。我的操作系统是yosemite,X代码是7.2。您不需要批量删除,您只需迭代获取的结果并删除每个结果,作为替代。我会试试看。我的操作系统是yosemite,X代码是7.2。你不需要批量删除,你只需要迭代获取的结果并删除每个,作为替代,这正是我所做的,它工作得非常好。但我做了类似的事情<代码>NSMutableDictionary*allUsers=[[managedObjectContext executeFetchRequest:请求错误:nil]mutableCopy];对于(NSManagedObject*theUser in alluser){[managedobject context deleteObject:theUser];}这正是我所做的,它工作得非常好。但我做了类似的事情<代码>NSMutableDictionary*allUsers=[[managedObjectContext executeFetchRequest:请求错误:nil]mutableCopy];对于(NSManagedObject*allUsers中的用户){[ManagedObject上下文删除对象:theUser];}