2

问题是即使使用 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 FROM ZTASK t0 在哪里 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:注释:从数据库中完成的故障:0x1f540670

为什么在设置属性时它会发生故障,我该如何避免这种情况?

先谢谢了!

4

0 回答 0