简体   繁体   中英

How to keep entity properties data after save it on core data

I'm creating an entity of UserEntity:

    let userEntity = UserEntity(context: managedContext)

    userEntity.username = user.username
    userEntity.name = user.name
    userEntity.personCode = user.personCode
    userEntity.personalData = XMLRegistrationData.toEntity(userPersonalData: user.registrations.registrationData)

and saving it on this property "loggedUser":

            let loggedUser = XMLUserData.toEntity(user: xmlUserData)
            print(loggedUser) // first print

            do {
                try self.context.save()

                print(loggedUser) // second print
            } catch {
                ...
            }

The first print results on this:

<UserEntity: 0x282b07a70> (entity: UserEntity; id: 0x28081c460 <x-coredata:///UserEntity/t78B51567-8104-4232-9BEA-062FBCB3CEEC3> ; data: {
    activeSession = nil;
    name = "FOO";
    personCode = 000;
    personalData = "0x280828020 <x-coredata:///UserPersonalDataEntity/t78B51567-8104-4232-9BEA-062FBCB3CEEC4>";
    username = 001;
})

But the second print result on this:

<UserEntity: 0x282b07a70> (entity: UserEntity; id: 0xd000000000840002 <x-coredata://C833ECD4-5D54-48C3-9860-B2E0781D08EC/UserEntity/p33> ; data: <fault>)

The UserEntity attributes is nil after saving it on the core data. The object is saved on the core data, I can fetch it using context.fetch, but loggedUser is changed.

Why that occurs? And how can I keep the data on loggedUser after it's saving, not needing to fetch it from core data after save it?

It's not nil it's a fault . The data are still there. You can prove it by printing loggedUser.username

Faulting reduces your application's memory usage by keeping placeholder objects (faults) in the persistent store.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM