这是我的第一个Core Data项目,我需要有关加快我的获取请求的建议.
我的核心数据模型包含2个实体,Wells和Fluids. Wells有50,000条记录,Fluids有200万条记录.它们看起来如下.
Wells
nams
relation
wellsToFluids
Fluids
text1, text2, etc.
relation
fluidsToWells
Run Code Online (Sandbox Code Playgroud)
获取请求Wells非常快.获取请求Wells与Fluids通过复合谓词通过wellsToFluids关系访问的数据相结合是很慢的.而且,我在不同的谓词上看到意外的获取时间.
我正在根据用户选择构建复合谓词.但基本上情况如下
搜索Wells:
predicateWithFormat: @"(wellNumber == 1)"
Run Code Online (Sandbox Code Playgroud)
.001秒
流体搜索:
predicateWithFormat: @"(ANY wellsToFluids.text2 CONTAINS[c] stringToFind)"
Run Code Online (Sandbox Code Playgroud)
1.3秒 (在Mac模拟器上 - 在iPhone或iPad上真的很慢)
抓取Wells并Fluids:
predicateWithFormat: @"(wellNumber == 1) AND (ANY wellsToFluids.text1 CONTAINS[c] stringToFind)"
Run Code Online (Sandbox Code Playgroud)
3.2秒
获取Wells和多个Fluids属性:
predicateWithFormat: @"(wellNumber == 1) AND (ANY wellsToFluids.text1 CONTAINS[c] stringToFind) AND (ANY …Run Code Online (Sandbox Code Playgroud) 我有一个ID数组。我想获取ids数组中各项的核心数据实体。我们如何使用来实现NSFetchRequest呢?
此代码返回0个不正确的对象.但是,删除谓词时,获取请求将返回所有对象.
NSError *error = nil;
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:[self managedObjectContext]];
NSPredicate * pr = [NSPredicate predicateWithFormat:@"%K beginswith '%@' ",
@"FullName", searchText];
//NSPredicate * pr = [NSPredicate predicateWithFormat:@"PersonID == %@", searchText]; Works fine
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
[request setPredicate:pr];
NSArray * arr = [[self managedObjectContext] executeFetchRequest:request error:&error];
Run Code Online (Sandbox Code Playgroud)
FullName属性包含unicode数据(阿拉伯语).
任何帮助表示赞赏.
我对Core Data非常陌生,而且我仍然对它充满了可能性.
我的模型中有一个表USER,我想将所有行作为"SELECT*FROM USER"获取.
哪种方法最好?
非常感谢您的帮助.
我运行我的应用程序,然后获取我的数据.数据还可以.当我第二次跑步时,我的旧价值出现了问题.怎么了?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Test" inManagedObjectContext:[self managedObjectContext]];
for (int i =0; i<2; i++)
{
Test *test = [[[Test alloc] initWithEntity:entity insertIntoManagedObjectContext:[self managedObjectContext]] autorelease];
test.text = @"Text";
test.index = [NSNumber numberWithInt:i];
}
[self saveContext];
}
-(void) showValues
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Test" inManagedObjectContext:[self managedObjectContext]];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entity];
NSError *error;
NSArray *array = [[self managedObjectContext] executeFetchRequest:request error:&error];
NSLog(@"Array: %@ ", array);
}
Run Code Online (Sandbox Code Playgroud)
第一次运行
2012-01-22 21:48:52.092 Mew[411:707] Array: ( …Run Code Online (Sandbox Code Playgroud) 来自apple doc 修改获取请求我看到可以更改NSFetchRequestfor NSFetchedResultsController.步骤很容易设置.
调用后performFetch:我觉得有必要调用reloadData表视图.如何进行这样的通话?
阅读一些stackoverflow主题,我已经看到调用该方法在大多数情况下应该工作.但有没有正确的方法呢?
在如何以编程方式切换UITableView的NSFetchedResultsController(或其谓词)?,TechZen写道:
只需确保在交换控制器之前将tableview本身发送为beginUpdates,然后在完成后再发送endUpdates.这可以防止表在换出FRC时在窄窗口中询问数据.然后调用reloadData.
你能解释一下这究竟是什么意思吗?
uitableview nsfetchedresultscontroller nsfetchrequest reloaddata ios
我就这个主题提出几个问题,但仍然无法使其发挥作用.我有一个核心数据,有10k +行的人名,我在tableview中显示.我希望能够用每个字母搜索和更新表格.这是非常迟钝的.正如我所建议的那样,我观看WWWDC '10核心数据演示并试图实施
[request setFetchBatchSize:50];
Run Code Online (Sandbox Code Playgroud)
似乎没有用.当我使用工具检查核心数据时,它仍然显示在加载tableview时仍有10k请求,当我搜索它时也获得所有结果.是否还需要做任何其他事情来设置批量大小或那些不会对我有帮助的东西.
唯一可行的方法是在搜索时将fetchlimit设置为100.你认为这是一个很好的解决方案吗?
提前致谢!
我花了几个小时试图获取工作.我需要摆脱重复,所以我认为我可以遵循这个指南核心 - 数据 - 如何做一个选择 - 不同 但它总是给我一个空数组.
这是我的代码:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"RBTag" inManagedObjectContext:[self context]];
request.entity = entity;
request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"name"]];
request.returnsDistinctResults = YES;
request.resultType = NSDictionaryResultType;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSError *error = nil;
NSArray *distincResults = [[self context] executeFetchRequest:request error:&error];
NSLog(@"Result: %@", distincResults);
Run Code Online (Sandbox Code Playgroud)
我没有错.我检查了NSPropertyDescription,它找到了我的财产.如果我评论出来:
request.resultType = NSDictionaryResultType;
Run Code Online (Sandbox Code Playgroud)
然后我得到结果,但它并不明显.=(
我一直在搜索Apple的Predicate Programming Guide和SO,试图确定为某个获取请求编写谓词的正确方法.
看起来我尝试使用点符号来遍历关系的方式似乎没有得到请求的尊重.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"project.user == %@", self.user];
[fetchRequest setPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)
用户,项目和任务都是映射到BaaS提供商(StackMob)的核心数据实体.
关系如下:用户 一对多 项目 一对多 任务与您期望的反向关系.我要做的是获取当前登录用户的所有任务.所以,我的想法是:发送一个带有实体"任务"的获取请求.拥有该任务的项目由登录用户拥有,获取该任务并显示它.很简单.
我想我应该能够在上面显示的谓词中使用点符号,但是获取请求返回nil.
关于获取请求的其他所有内容都很好.如果我将格式字符串更改为@"project != NULL",我将恢复所有任务.
我只是错误地写了我的格式字符串吗?向StackMob发送获取请求时,点符号在某种程度上无效吗?
一如既往,非常感谢任何帮助.
如果需要更多信息,请告诉我.我现在有点太接近这一点,看得很清楚.
我正在尝试基于“ objectID”访问NSManagedObject。从我的读物中,每个NSManagedObject都会有一个唯一的“ objectID”。但是,当我尝试基于“ objectID”访问对象时,出现以下错误。
CoreData:错误:SQLCore dispatchRequest:异常处理请求:,在userInfo为(null)的实体中找不到键路径objectID
func loadPlayerDetailsFromCoreData() {
let fetch = Player.fetchRequest() as NSFetchRequest
// Create Predicate
let predicate = NSPredicate(format: "objectID == %@", selectedPlayerID!)
fetch.predicate = predicate
do {
let player = try appDelegate.coreDataStack.managedObjectContext.fetch(fetch)
print("Player selected = \(String(describing: player.first?.name))")
} catch {
print("Failed")
}
}
Run Code Online (Sandbox Code Playgroud) nsfetchrequest ×10
core-data ×8
ios ×8
nspredicate ×4
iphone ×3
objective-c ×2
swift ×2
arrays ×1
ios6 ×1
large-files ×1
reloaddata ×1
select ×1
stackmob ×1
subquery ×1
uitableview ×1