我们如何检索在 iOS 中使用核心数据保存的数据
Posted
技术标签:
【中文标题】我们如何检索在 iOS 中使用核心数据保存的数据【英文标题】:How do we Retrive Data Which is Saved By Using Core Data in iOS 【发布时间】:2014-01-03 10:38:09 【问题描述】:我是 Core Data 的新手。我已经开发了一个应用程序。在我的应用程序中,我有注册、登录、忘记密码表格。
在注册表中我写这个代码来注册。
NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
NSManagedObject *newuser = [[NSManagedObject alloc]initWithEntity:entitydesc insertIntoManagedObjectContext:context];
[newuser setValue:self.registerUsername.text forKey:@"username"];
[newuser setValue:self.registerPassword.text forKey:@"password"];
[newuser setValue:self.registerConfirmPassword.text forKey:@"confirmpassword"];
[newuser setValue:self.registerMobileNumber.text forKey:@"mobilenumber"];
NSError *error;
[context save:&error];
在登录表单中我写了这段代码:
NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username like %@ and password like %@",self.logUsername.text,self.logPassword.text];
[request setPredicate:predicate];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
if(matchingData.count<=0)
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error!" message:@"Username and password does not match" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
else
NSString *username;
NSString *password;
for(NSManagedObject *obj in matchingData)
username = [obj valueForKey:@"username"];
password = [obj valueForKey:@"password"];
SecurityHome *securityhome = [[SecurityHome alloc]init];
[self.navigationController pushViewController:securityhome animated:YES];
两者都工作正常。如果用户输入的用户名和手机号码正确,我现在想在忘记密码页面显示密码。我怎样才能做到这一点?
【问题讨论】:
希望永远不会。您最好存储加密的密码,如果用户忘记了他的密码,您可以向他发送一个新密码。但我不会使用将我的纯密码存储在数据库中的应用程序...... 我支持 Dehlen。你真的不应该这样做。无论如何,您可能需要此信息用于其他目的,所以我已经回答了您的问题 【参考方案1】:设置示例谓词和排序顺序...
NSNumber *minimumSalary = ...;
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(lastName LIKE[c] 'Worsley') AND (salary > %@)", minimumSalary];
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"firstName" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil)
// Deal with error...
【讨论】:
【参考方案2】:在问这个问题之前,您应该先进行一些“谷歌搜索”。无论如何,让我为你做。给你。Core Data NSFetchResult guide for developer Ray wenderlich- Really nice tutorial
在列出的两个链接中,我都在尝试推荐 NSFetchResultControllers,因为它们既快速又简单。在这里你可以看到不同之处 没有 NSFetchResultController, NSManagedObjectContext *moc = [self managedObjectContext]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:moc]; NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; [请求 setEntity:entityDescription];
// Set example predicate and sort orderings...
NSNumber *minimumSalary = ...;
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(lastName LIKE[c] 'Worsley') AND (salary > %@)", minimumSalary];
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"firstName" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil)
// Deal with error...
使用 NSFetchresultController
[[self managedObjectContext] fetchObjectsForEntityName:@"Employee" withPredicate:
@"(lastName LIKE[c] 'Worsley') AND (salary > %@)", minimumSalary];
这些示例适用于 Famous 员工表。根据您的要求翻译代码。如果您因任何原因无法使用此代码,请告诉我..
【讨论】:
【参考方案3】:You can use this code for fetching details and pass the object to Forgot class and show there.
-(User*)getUserCredentialDetails:(NSUInteger)mobilenumber
NSError *error = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"mobilenumber = %d",mobileNumber];
[request setPredicate:predicate];
NSArray * resultArray = [managedObjectContext executeFetchRequest:request error:&error];
[request release];
if (error != nil)
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
return [resultArray lastObject];
【讨论】:
以上是关于我们如何检索在 iOS 中使用核心数据保存的数据的主要内容,如果未能解决你的问题,请参考以下文章