我想NSFetchRequest按实体对结果进行分组.实体都共享相同的抽象父级.例如:
animal
|
|-cat
|
|-dog
Run Code Online (Sandbox Code Playgroud)
在NSFetchRequest已includesSubentities设定TRUE和entity设定animal.它可以设置sectionNameKeyPathNSFetchedResultsController来的entity.name,但它是不可能做的一样sortDescriptors的的NSFetchRequest由于事实sortDescriptors是(在数据库中的数据,即,在不类方法)应用到存储的属性.因此,按实体类型分组的唯一方法是向超类添加属性,子类可以使用该属性来标识自己.
这似乎很疯狂,因为它破坏了继承的有用性.我查看了SQLite数据库,实体类型与属性存储在同一个表中,因此所需的数据已经存在.
总结:是否可以在NSFetchRequest不添加其他属性的情况下按子类排序?
如何NSPredicate按类的实体创建过滤器Contact?
NSPredicate的解决方案检查对象类崩溃的类型:
[NSPredicate predicateWithFormat:@"person.class == %@", [Contact class]];
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'keypath person.class not found in entity <NSSQLEntity User id=12>'
Run Code Online (Sandbox Code Playgroud)
我有核心数据实体User(app用户)和Contact(地址簿联系人),两个孩子Person(抽象实体).Person有很多identities,可以是emails,phones或usernames.
要创建类似文本消息的typeahead,我想创建一个NSFetchRequestfor 类的Identity实体.personContact
class core-data nspredicate nsfetchrequest nsentitydescription
我正在解析大量数据,我最初将其插入到核心数据存储中.
稍后,我正在解析相同的XML,尽管其中一些可能已经更新.然后我做的是检查具有相同标签的现有记录,如果已经存在,我用数据更新记录.
然而,虽然我的初始解析(约11.000条记录)需要8秒左右,但更新似乎很昂贵,需要144秒(这些是模拟器运行,因此在实际设备上显着更长).
虽然第一次很好(我正在显示一个进度条),但第二次是不可接受的长,我想做一些事情来提高速度(即使它发生在一个单独的线程的后台).
不幸的是,这不是查找或创建的问题,因为XML中的数据可能已针对单个记录进行了更改,因此每个人都可能需要更新.
我对属性进行了索引,这加快了初始解析和更新,但它仍然很慢(上面的数字是索引).我注意到解析/更新似乎逐渐减慢.虽然最初速度很快,但随着越来越多的记录被处理,它会越来越慢.
所以最后我的问题是,如果我有什么建议可以提高我更新数据集的速度?我正在使用MagicalRecord获取记录.这是代码:
Record *record;
if (!isUpdate) {
record = [NSEntityDescription insertNewObjectForEntityForName:@"Record" inManagedObjectContext:backgroundContext];
} else {
NSPredicate *recordPredicate = [NSPredicate predicateWithFormat:@"SELF.tag == %@", [[node attributeForName:@"tag"] stringValue]];
record = [Record findFirstWithPredicate:recordPredicate];
}
Run Code Online (Sandbox Code Playgroud) 如何设置获取请求以仅从具有一个特定值的实体属性中提取数据?这是我以前用过的基本代码.
-(void)fetchResults
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:self.entityDescription.name];
NSString *cacheName = [self.entityDescription.name stringByAppendingString:@"Cache"];
// predicate code
if (self.predicate != nil) {
[fetchRequest setPredicate:self.predicate];
}
// end of predicate code
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:cacheName];
BOOL success;
NSError *error;
success = [self.fetchedResultsController performFetch:&error];
if (!success)
{
NSLog(@"%@", [error localizedDescription]);
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在看这个页面:http://bit.ly/KevYwR这是正确的方向吗?
我需要使用NSPredicate还是可以不使用?
感谢您的帮助,指出正确的方向等.
我想创建NSFetchRequest和设置属性以获取如下:
request.propertiesToFetch = @[@"a", @"b", @"c"];
Run Code Online (Sandbox Code Playgroud)
其中a和b存储在Core Data数据库中,并且c是瞬态的.executeFetchRequest:发出错误Invalid keypath c passed to setPropertiesToFetch:.但如果我取消选中属性的transient-checkbox,c一切都会正常工作.那么获取瞬态属性真的不可能,或者我做错了什么?
我是Core Data的noob,我在Apple开发者网站上找不到我需要的帮助.
我在我的.xcdatamodeld文件中定义了一个获取请求,但现在我找不到如何使用它了?这是获取请求的定义:

我认为获取请求始于:
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Category")
Run Code Online (Sandbox Code Playgroud)
但我不知道如何通过名称使用获取请求.(该项目是用Swift 3编写的)
我正在开发和应用程序,需要检查字符串是否已保存到数据库.这似乎是一个简单的操作,但它需要半秒才能返回任何我认为非常多的响应.我的问题是,是否有任何方法可以缩短时间.谢谢你的关注.
这是我目前的代码:
- (BOOL) isDeleted:(int)i {
NSString *value = [NSString stringWithFormat:@"deleted.*.%i", i];
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSString *entityName = @"Deleted";
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(deletedpics like %@)", value];
[request setPredicate:pred];
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
BOOL returnBool = [objects count] >= 1 ? YES : NO;
return returnBool;
}
Run Code Online (Sandbox Code Playgroud) 我有一个 Entity 被调用,deal并且deal有一个属性被调用date,这是这个交易对象插入商店的时间。
一天可能有好几个deals。
所以我想按天计算一些数据组,我想获取day并countofsomething
喜欢:
2013-06-03 3
2013-06-02 4
Run Code Online (Sandbox Code Playgroud)
我不想使用,sectionPath因为它只将交易放入部分。
我知道我能有这个工作由有另一个属性(类型:字符串)喜欢dayOfTheDate这就像2013-06-03在每个对象。
顺便说一句,瞬态属性在这种情况下似乎不起作用
你能明白我在找什么吗?
在此处发表评论,以便我提供更多详细信息
感谢大家。
macos core-data nsfetchedresultscontroller nsfetchrequest ios
我有一个核心数据应用程序,其模型设置如下图所示:
关系设置如下:
类别<-->>子类别<-->>项目
现在,是否可以在一次提取中从具有名为“categoryName”的相同属性的 Subcategory 和 Item 实体获取对象?我认为可以通过使用关系来完成,但我使用的代码不起作用,即:
let fetchNote = NSFetchRequest(entityName: "Category")
fetchNote.predicate = NSPredicate(format: "ANY subcategory.name == %@ AND item.name == %@", "Badaue", "Badaue")
let notes = try! appDel.managedObjectContext.executeFetchRequest(fetchNote) as? [Category]
print(notes)
Run Code Online (Sandbox Code Playgroud) 我正在使用 NSPersistentContainer 访问 iOS10 应用程序和 Xcode 8 中的核心数据。与 Entity 类似,我在 Coredata xcdatamodel 中添加了“UserFetchRequest”。下面是屏幕截图。
如何访问代码中的“UserFetchRequest”?
core-data ×10
nsfetchrequest ×10
ios ×8
nspredicate ×2
objective-c ×2
attributes ×1
class ×1
iphone ×1
macos ×1
optimization ×1
request ×1
swift ×1
swift3 ×1
xcdatamodel ×1