iPhone核心数据保存不起作用

发布于 2024-09-10 10:34:01 字数 1407 浏览 7 评论 0原文

我尝试使用 Core Data 保存 Device 类(自定义类)列表并检索它。但是保存后,我的查询(非常简单)不会返回任何记录。

我对保存记录的调用始终返回 YES 并且没有错误:

BOOL resultOfSave = [managedObjectContext save:&err];

我通过属性 userLogin 进行搜索的谓词如下所示:

- (NSArray *) devicesForUser:(NSString *)username 
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Device" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userLogin = %@", username];
[request setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"macAddress" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];

NSError *error = nil;
NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&error];
if (fetchResults == nil) {
    // Handle the error.
    NSLog(@"ERROR: nil fetch result array");
}

[request release];
NSLog(@"devicesForUser [%@] count %d", username, [fetchResults count]);
return fetchResults;    
}

但我总是得到零结果,如果我摆脱谓词,我会得到所有对象,并且如果我循环通过它们,我可以看到至少一个对象的 userLogin 属性设置为我传入的用户名...

有人遇到过这样的问题吗???

感谢您可以给

马克的任何帮助

Im trying to save a list of Device classes (a custom class) using Core Data and retrieve it. But After I save it, my query, which is VERY simple, doesnt return any records.

My call to save the records always returns YES and no errors:

BOOL resultOfSave = [managedObjectContext save:&err];

My predicate for searching by the property userLogin is like so:

- (NSArray *) devicesForUser:(NSString *)username 
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Device" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userLogin = %@", username];
[request setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"macAddress" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];

NSError *error = nil;
NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&error];
if (fetchResults == nil) {
    // Handle the error.
    NSLog(@"ERROR: nil fetch result array");
}

[request release];
NSLog(@"devicesForUser [%@] count %d", username, [fetchResults count]);
return fetchResults;    
}

but I always get zero results, if I get rid of the predecate, I get all the objects, and if I loop through them I can see that the userLogin property for at least one of the object IS set to the username that I pass in...

Has anyone ever had an issue like this???

Thanks for any help you can give

Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见 2024-09-17 10:34:01

在您的谓词中,将:

@"userLogin = %@"

...更改为:

@"userLogin == %@"

In your predicate, change:

@"userLogin = %@"

...to:

@"userLogin == %@"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文