-1

我正在从某个实体获取一组托管对象。在 executeFetchRequest 返回 I NSLog 元素及其属性之后,一切都很好。在我返回新生成的数组并尝试在后台线程或其他方法中使用它之后,数组内的托管对象的属性变为 nil。这是代码:

Utakmice -NSManagedObject 子类

    - (NSArray*)ucitajPodatke:(NSDate*)zaDatum drzavaId:(int)_drzavaId
    {

            NSManagedObjectContext *con = [[NSManagedObjectContext alloc] init];
            [con setPersistentStoreCoordinator:persistentStoreCoordinator];
            [con setStalenessInterval:0];
            [con setUndoManager:nil];
            // create request and predicate
            // set return result type to NSManagedObjectResultType
            ...
            ...

        return fetchedObjects; -> This works cause I can log everything and all values R OK...
    }

    -(void)SomeMethod
        NSArray *array  = [self ucitajPodatke:danas drzavaId:self.drzavaId];

        Utakmice *tekma = [array objectAtIndex:0];
        NSLog(@"%i", [tekma.uniqueId intValue]); -> everything is fine


        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.35 * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

                if (array != nil && [array count]>0)
                {
                    Utakmice *tekma1 = [array objectAtIndex:0];
                    DLog(@"%@", tekma1.uniqueId);
                    DLog(@"%i", [tekma1.uniqueId intValue]); -> all properties have nil value
    ...
    ...

有什么建议么??我真的不知道从这里去哪里......

还有一件事。在 ucitajPodatke 方法中,当我将返回结果类型设置为 NSDictionaryResultType -> 一切正常...(我需要托管对象,因为我需要关系)... Thx in advanced

4

2 回答 2

0

您需要做的第一件事是保持获取数组的强引用,然后您可以进行调度。

那会很好打电话

  [self performSelector:@selector(someMethod) withObject:nil afterDelay:0.35];

而不是派遣。

This issue also can be related with "Data faulting". The data is being fetched when you access to object fields. If you want to fully fetch objects without faulting you can use

NSFetchRequest *request = ...;// your fetch request here
[request setReturnsObjectsAsFaults:YES];
// Fetch here
于 2013-11-05T15:46:28.387 回答
-1

Ok I finally google it: Core Data - sharing NSManagedObjects among multiple threads

It appears that U can not pass managed objects between threads, Instead U should pass managed object id's

于 2013-11-05T15:55:41.840 回答