Permanent NSManagedObjectID not so permanent?

后端 未结 2 1034
北恋
北恋 2020-12-05 08:23

I\'m having trouble dealing with object IDs in CoreData. I\'m using MagicalRecord for convenience and have 3 contexts: a private queue working context, a main queue context

2条回答
  •  我在风中等你
    2020-12-05 09:21

    This may be because you're using nested context.

    #import 
    #import 
    
    static NSArray *fetchAllPersons(NSManagedObjectContext *moc);
    static NSManagedObjectModel *managedObjectModel();
    static NSManagedObjectContext *createManagedObjectContext();
    static NSURL *desktopDirectoryURL(void);
    
    static void testObjectID(void);
    
    
    
    int main(int argc, const char * argv[])
    {
        @autoreleasepool {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                testObjectID();
            });
        }
        dispatch_main();
        return 0;
    }
    
    
    
    
    static void testObjectID(void)
    {
        NSManagedObjectContext *c = createManagedObjectContext();
        [c performBlock:^{
    
            NSArray *all = fetchAllPersons(c);
            NSLog(@"count: %lu", all.count);
            NSLog(@"all accounts = %@", all);
    
            NSManagedObject *a = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:c];
            [a setValue:@"foo" forKey:@"name"];
    
            NSLog(@"temp a.objectID = %@", a.objectID);
            NSError *error = nil;
            NSCAssert([c obtainPermanentIDsForObjects:@[a] error:&error], @"perm id error: %@", error);
            NSLog(@"perm a.objectID = %@", a.objectID);
    
            NSCAssert([c save:&error], @"Save failed: %@", error);
    
            NSURL *u = a.objectID.URIRepresentation;
    
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                NSManagedObjectContext *d = createManagedObjectContext();
    
                NSArray *all = fetchAllPersons(c);
                NSLog(@"count: %lu", all.count);
                NSLog(@"all accounts = %@", all);
    
                NSManagedObjectID *i = [d.persistentStoreCoordinator managedObjectIDForURIRepresentation:u];
                NSError *objWithIdError = nil;
                NSManagedObject *o = [d existingObjectWithID:i error:&objWithIdError];
                NSCAssert(o != nil, @"existing object error: %@", objWithIdError);
    
                NSLog(@"o = %@", o);
                NSLog(@"o.objectID = %@", o.objectID);
            });
        }];
    }
    
    
    static NSArray *fetchAllPersons(NSManagedObjectContext *moc)
    {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
        NSError *error = nil;
        NSArray *result = [moc executeFetchRequest:request error:&error];
        NSCAssert(result != nil, @"Fetch failed: %@", error);
        return result;
    }
    
    static NSManagedObjectModel *managedObjectModel()
    {
        static NSManagedObjectModel *mom = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSEntityDescription *personEntity = [[NSEntityDescription alloc] init];
            [personEntity setName:@"Person"];
    
            NSAttributeDescription *nameAttribute = [[NSAttributeDescription alloc] init];
    
            [nameAttribute setName:@"name"];
            [nameAttribute setAttributeType:NSStringAttributeType];
    
            [personEntity setProperties:@[nameAttribute]];
    
            mom = [[NSManagedObjectModel alloc] init];
            [mom setEntities:@[personEntity]];
        });
        return mom;
    }
    
    
    static NSManagedObjectContext *createManagedObjectContext()
    {
        static NSPersistentStoreCoordinator *coordinator;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel()];
    
            NSString *STORE_TYPE = NSSQLiteStoreType;
            NSString *STORE_FILENAME = @"foobar.db";
    
            NSError *error;
            NSURL *url = [desktopDirectoryURL() URLByAppendingPathComponent:STORE_FILENAME];
    
            NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE
                                                                    configuration:nil URL:url options:nil
                                                                            error:&error];
    
            if (newStore == nil) {
                NSLog(@"Store Configuration Failure\n%@",
                      ([error localizedDescription] != nil) ?
                      [error localizedDescription] : @"Unknown Error");
            }
        });
    
        NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        [moc setPersistentStoreCoordinator:coordinator];
    
        return moc;
    }
    
    
    static NSURL *desktopDirectoryURL(void)
    {
        static NSURL *URL;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSFileManager *fileManager = [[NSFileManager alloc] init];
            NSError *error;
            URL = [fileManager URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
            NSCAssert(URL != nil, @"Could not access Desktop directory: %@", [error localizedDescription]);
        });
        return URL;
    }
    

    Outputs:

    count: 0
    all accounts = (
    )
    temp a.objectID = 0x10180e640 
    perm a.objectID = 0x101901bb0 
    count: 1
    all accounts = (
        " (entity: Person; id: 0x101901bb0  ; data: {\n    name = foo;\n})"
    )
    o =  (entity: Person; id: 0x100415b60  ; data: {
        name = foo;
    })
    o.objectID = 0x100415b60 
    

提交回复
热议问题