setPropertiesToFetch:和setReturnsObjectsAsFaults:NO与关系

时间:2013-05-09 23:11:47

标签: core-data relationships fault

问题是即使使用setPropertiesToFetch:和setReturnsObjectsAsFaults:NO设置关系也会在我的sqlite3数据库上引起2个额外的“select ...”。

我有一个任务和一个工作模型,它们之间有关系。任务有一个作业,job_id和作业有一个任务。我导入了很多任务和工作。为了加快处理速度,我首先导入所有任务和作业,然后再设置关系。导入工作正常。当我设定关系出乎意料地发生。我使用以下代码来设置关系。

int limit = 100;
NSManagedObjectContext *moc = [appDelegate newMoc];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Task" inManagedObjectContext:moc]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"job = nil"]];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setFetchLimit:limit];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"job", @"job_id", nil]];
[fetchRequest setShouldRefreshRefetchedObjects:NO];
[fetchRequest setIncludesSubentities:NO];

while (true) {
    @autoreleasepool {
        NSError *error = nil;
        NSArray *tasks = [moc executeFetchRequest:fetchRequest error:&error];           
        if (!error) {
            if (tasks.count > 0) {

                NSMutableSet *jobIDs = [[NSMutableSet alloc] init];                 
                for (Task *task in tasks) {                     
                    [jobIDs addObject:task.job_id];
                }                   

                NSArray *jobs = [ListObjectsManager objectsForIDs:jobIDs properties:[NSArray arrayWithObjects:@"baseObjectID", @"task", nil] entityName:JobClassName moc:moc];
                [jobIDs release];
                for (Task *task in tasks) {

                    for (Job *job in jobs) {

                        if (task.job_id.intValue == job.baseObjectID.intValue) {
                            task.job = job;
                            job.task = task;
                            break;
                        }
                    }
                }
                [appDelegate saveContext:moc];
            }
            else {
                //No more relationships to set
                break;
            }
        }
        else {
            NSLog(@"ERROR FETCHING : %@", error.localizedDescription);
        }
    }
}

[fetchRequest release];

task.job = job 这一行创建了2个新的“select ...”!?!

这产生了许多我想避免的选择。


加成

NSArray *tasks = [moc executeFetchRequest:fetchRequest error:&error];

创建“ SELECT t0.Z_ENT,t0.Z_PK,t0.ZJOB,t0.ZJOB_ID FROM ZTASK t0 WHERE t0.ZJOB IS NULL LIMIT 100 ”“按预期工作。

NSLog(@"%d", [tasks objectAtIndex:0].job_id.intValue);

没有创建新的“选择...”,它按预期工作。

[tasks objectAtIndex:0].job_id = [NSNumber numberWithInt:63];

创建:

2013-05-10 00:26:59.048 [1197:1803] CoreData:sql:SELECT 0,t0.Z_PK,t0.Z_OPT,t0.ZBASEOBJECTID,t0.ZBASEOBJECTNAME,t0.ZJOB_DESCRIPTION,t0.ZJOB_ID,t0 .ZJOB来自ZTASK t0 WHERE t0.Z_PK =? 2013-05-10 00:26:59.058 [1197:1803] CoreData:注释:sql连接获取时间:0.0099s 2013-05-10 00:26:59.061 [1197:1803] CoreData:注释:总获取执行时间:1行0.0133秒。 2013-05-10 00:26:59.064 [1197:1803] CoreData:annotation:从数据库中完成的错误:0x1f540670

为什么设置属性时会出现故障,我该如何避免?

先谢谢!

0 个答案:

没有答案