这是在iOS上.
我有一个核心数据库,有大约35万个对象.对象(Product)有两个属性:"条形码"和"指定".用户可以通过搜索"条形码"来搜索对象,并且应该返回"指定".一切都很好,除了它很慢.我使用的代码是:
NSEntityDescription *_product = [NSEntityDescription entityForName:@"Product" inManagedObjectContext:importContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
[fetch setEntity:_product];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"Barcode == %@",theBarcode]];
NSError *error = nil;
NSArray *results = [importContext executeFetchRequest:fetch error:&error];
NSManagedObject *object = [results objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
由于我只想获取一个对象,有没有办法加快速度?
如果我在启动时将每个对象加载到一个数组中,那么应用程序的启动速度非常慢,占用大量内存.
提前致谢!
编辑:我添加了[fetch setFetchLimit:1]; 这加快了一点点.但是数据库中对象的速度越来越慢.
如何编写NSPredicate来获取与数组中的值匹配的行?例如:
[NSPredicate predicateWithFormat:@"UserName IN %@",UserIDArray];
Run Code Online (Sandbox Code Playgroud)
如果UserIDArray包含用户ID,如何获取与此数组中的值匹配的用户名?
我是Core Data的新手,我设计了一个基于导航的应用程序,我使用的一些数据是在运行时创建的(来自URL通过JSON).我花了几天时间搜索了一些教程,但还没有意识到如何将传入的JSON数据保存到我的核心数据模型中的实体(或事件?)中.我获取DetailViewController类中的数据,我需要将此数据保存到Core Data(我已经准备了一个具有7个属性的实体).任何人都可以帮忙吗?(如果你知道一个很好的教程或示例代码,我会很高兴)
编辑这可能有点具体,但我真的遇到麻烦,只需要一些帮助.我的数据来自一种安静的服务器(我用PHP编写),首先是用户输入他/她的登录信息(我之前保存到服务器上的数据库),当响应数据到来时我将使用不同的不同视图中的元素(例如,user_id将在视图上使用,而buttonData在其他视图上使用等).我的问题是,如何将JSON数据保存到我的核心数据模型中(目前有树实体).提前致谢
注意:我很喜欢周围很多,但找不到像我这样的应用程序的任何答案和教程
我有一个NSEntity(Cloth)与属性(type这是从预定义的列表填充的)NSString值,即雨披,夹克,外套,袍子,顶,西服等
我想获取所有Cloth记录,以便根据已定义的顺序对记录进行排序type.即所有的雨披记录都在上面,然后是夹克,然后是外套,然后是礼服,然后是Top,然后是西装等.
Poncho? …
Poncho? …
Poncho? …
Jacket …
Jacket …
Coat …
Coat …
Coat …
Gown …
Gown …
Top …
Top …
Top …
Top …
Suit …
Suit …
Run Code Online (Sandbox Code Playgroud)
此订单不是升序或降序,而是自定义.如何获取Cloth记录以便以此(定义)顺序返回它们?
我的NSFetchedResultsController有问题,似乎是通过打开IncludesPendingChanges来解决的,这让我很担心.(我发现这不是真的,包括未决的更改也无济于事.)
会发生什么是我的Fetched Results Controller在完整的API刷新时正确地获取并显示我的对象.单步执行,我已经确认这个完整的API刷新的临时上下文被保存并合并而没有错误.
但是,如果我返回到从不同导航流执行相同获取请求的视图,则只返回并显示一个部分完成的对象,而不是完整的对象集.如果我此时执行完整的API刷新,则刷新将显示正确的对象.
我相信我的问题可能是mergeChangesFromContextDidSaveNotification没有传播到主上下文,但从我的日志和单步执行,在我看来,它正在保存正确.我不知道从哪里开始.虽然IncludesPendingChanges修复了症状,但我不认为它解决了潜在的问题.
作为一些补充信息,我将这个框架用于我的核心数据管理:https://github.com/vokalinteractive/CoreDataManager-iOS
我通过在VIFetchedResultsController.m的第156行添加来启用includesPendingChanges
[fetchRequest setIncludesPendingChanges:YES]
Run Code Online (Sandbox Code Playgroud)
编辑无论侥幸让我相信includePendingChanges为YES修复问题不再存在.事实证明,这个项目有一些严重的内存管理,并且花了一整天的时间来清理它,我仍然没有接近这个工作.尽管如此,它看起来似乎没有传播到主数据存储区,或者没有坚持下去.
作为一个提示,就像我在下面的评论中提到的,即使我指定"includePendingChanges:YES",也可以使用以下命令记录我提取的对象:
[self.fetchedResultsController.fetchedObjects description]
Run Code Online (Sandbox Code Playgroud)
以"includesPendingChanges:NO"结尾.知道是什么原因引起的吗?
我有一个iOS应用程序,它使用Core Data来保存和检索数据.
如何按自然排序顺序获取按NSString类型字段排序的数据?
现在的结果是:
100_title
10_title
1_title
Run Code Online (Sandbox Code Playgroud)
我需要:
1_title
10_title
100_title
Run Code Online (Sandbox Code Playgroud) 我有一个表视图,我刚刚实现了一个类,可以帮助我重新排序单元格,就像表视图委托附带的常规移动单元格方法一样.
现在我重新排序单元格后,我需要将保存单元格对象的数组更改为新顺序...我该怎么做?
这是我重新排序单元格的方法:
- (void)moveTableView:(FMMoveTableView *)tableView moveRowFromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { NSArray
}
Run Code Online (Sandbox Code Playgroud)
我有一个coreDataStack类来处理所有核心数据(创建一个singelton),它看起来像这样:
#import "CoreDataStack.h"
@implementation CoreDataStack
#pragma mark - Core Data stack
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
+ (instancetype)defaultStack {
static CoreDataStack *defaultStack;
static dispatch_once_t onceTocken;
dispatch_once (&onceTocken, ^{
defaultStack = [[self alloc] init];
});
return defaultStack;
}
- (NSURL *)applicationDocumentsDirectory {
// The directory the application uses to store the Core Data store file. This code uses a directory named …Run Code Online (Sandbox Code Playgroud) objective-c uitableview nsfetchedresultscontroller nsfetchrequest ios
我遇到了Swift中的以下问题.
我编写了以下函数来检索模型中所有使用name"David"的人.
private func myFetchRequest()
{
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let myRequest = NSFetchRequest(entityName: "RegisterOfPersons")
myRequest.predicate = NSPredicate(format: "name = %@", "David")
do{
let results = try moc.executeFetchRequest(myRequest)
for result in results
{
print(result)
}
} catch let error{
print(error)
}
}
Run Code Online (Sandbox Code Playgroud)
即使我的模型包含两个attribute名为="David"的条目,该行
myRequest.predicate = NSPredicate(format: "name = %@", "David")
Run Code Online (Sandbox Code Playgroud)
找不到这些条目,因为"name"是我模型中的可选属性.我是否必须搜索Optional("David"),意思是什么myRequest.predicate = NSPredicate(format: "name = %@", Optional("David"))?
如何在模型中搜索名为"David"的所有条目?我究竟做错了什么?
谢谢你的帮助!
该文件对可可的NSFetchRequest fetchLimit财产说:
获取限制指定请求在执行时应返回的最大对象数.
如果设置了获取限制,框架会尽最大努力提高效率,但不能保证.
但是,如果已设置了提取限制,它不会告诉您如何取消限制请求应返回的最大对象数.
在先前限制了fetchLimit我的对象数量之后,如何返回无限数量的匹配对象NSFetchRequest?
我从我的应用程序中的核心数据中获取了一个结果,并且需要获取我给它的'capacity'属性的整数值.
'capacity'属性是我的核心数据模型中的NSInteger 16.
我得到的结果如下:
entity = [NSEntityDescription entityForName:@"TableInfo" inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name == %@", [NSString stringWithFormat:@"%@", [tablesArray objectAtIndex:row]]]];
NSArray *selectedTableFetched = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
Run Code Online (Sandbox Code Playgroud)
现在我的问题是如何从我的结果中获得"容量"值?我知道有一个结果,因为我检查了数组的计数,我试过这个:
NSNumber *n = [selectedTableFetched valueForKey:@"capacity"];
Run Code Online (Sandbox Code Playgroud)
它给了我一些东西,当访问NSNumber时,我得到一个无意义的长数,所以如果我尝试访问整数值,如下所示:
[n integerValue]
Run Code Online (Sandbox Code Playgroud)
它崩溃了应用程序,我收到此错误:
'NSInvalidArgumentException', reason: '-[__NSArrayI integerValue]: unrecognized selector sent to instance 0x138870'
Run Code Online (Sandbox Code Playgroud)
希望你能帮助我,我需要做的就是从容量属性中获取整数值,谢谢.
nsfetchrequest ×10
core-data ×9
ios ×7
objective-c ×4
iphone ×3
ipad ×2
swift ×2
cocoa ×1
ios5 ×1
natural-sort ×1
nspredicate ×1
sorting ×1
uitableview ×1