Data not saving to entities #141

Closed
opened 2025-12-29 18:23:29 +01:00 by adam · 9 comments
Owner

Originally created by @martheli on GitHub (May 25, 2017).

My code runs without error but when I try to print out the entity I receive the following print:

success()
[<Person: 0x600000088ac0> (entity: Person; id: 0xd000000000040000 <x-coredata://71CEC961-C7C3-4466-BD1B-DA19FA909453/Person/p1> ; data: <fault>)]

My code is simple and pretty much right from the readme but it looks like nothing it being added.

CoreStore.perform(
            asynchronous: { (transaction) -> Void in
                let person = transaction.create(Into<Person>())
                person.name = "John Smith"
                person.job = "Dentist"  },

            completion: { (result) -> Void in
                switch result {

                case .success: print(result)
                    
                case .failure(let error): print(error)
                    
                }
            let people = CoreStore.fetchAll(From<Person>())
            print (people!)
Originally created by @martheli on GitHub (May 25, 2017). My code runs without error but when I try to print out the entity I receive the following print: ``` success() [<Person: 0x600000088ac0> (entity: Person; id: 0xd000000000040000 <x-coredata://71CEC961-C7C3-4466-BD1B-DA19FA909453/Person/p1> ; data: <fault>)] ``` My code is simple and pretty much right from the readme but it looks like nothing it being added. ``` CoreStore.perform( asynchronous: { (transaction) -> Void in let person = transaction.create(Into<Person>()) person.name = "John Smith" person.job = "Dentist" }, completion: { (result) -> Void in switch result { case .success: print(result) case .failure(let error): print(error) } let people = CoreStore.fetchAll(From<Person>()) print (people!) ```
adam added the question label 2025-12-29 18:23:29 +01:00
adam closed this issue 2025-12-29 18:23:29 +01:00
Author
Owner

@JohnEstropia commented on GitHub (May 25, 2017):

Did you already add a store using addStorage()?

CoreStore.addStorage(
    SQLiteStore(fileName: "MyStore.sqlite"),
    completion: { (result) -> Void in
        // ...
    }
)

(See the Setting-up section on the README file)

@JohnEstropia commented on GitHub (May 25, 2017): Did you already add a store using `addStorage()`? ```swift CoreStore.addStorage( SQLiteStore(fileName: "MyStore.sqlite"), completion: { (result) -> Void in // ... } ) ``` (See the [Setting-up](https://github.com/JohnEstropia/CoreStore#setting-up) section on the README file)
Author
Owner

@martheli commented on GitHub (May 25, 2017):

Yes I have this above:

try CoreStore.addStorageAndWait()

catch
{
print(error)
}

@martheli commented on GitHub (May 25, 2017): Yes I have this above: ``` try CoreStore.addStorageAndWait() catch { print(error) } ```
Author
Owner

@JohnEstropia commented on GitHub (May 25, 2017):

@martheli Sorry, I just realized you were fetching the object right after calling an asynchronous transaction. perform(asynchronous:...) executes on a background thread, so the object will not be created yet right after the call. You can try fetching inside the completion closure instead:

CoreStore.perform(
            asynchronous: { (transaction) -> Void in
                let person = transaction.create(Into<Person>())
                person.name = "John Smith"
                person.job = "Dentist"  },

            completion: { (result) -> Void in
                switch result {

                case .success:
                    let people = CoreStore.fetchAll(From<Person>())
                    print (people!)
                    
                case .failure(let error): print(error)
                    
                }
@JohnEstropia commented on GitHub (May 25, 2017): @martheli Sorry, I just realized you were fetching the object right after calling an asynchronous transaction. `perform(asynchronous:...)` executes on a background thread, so the object will not be created yet right after the call. You can try fetching inside the `completion` closure instead: ```swift CoreStore.perform( asynchronous: { (transaction) -> Void in let person = transaction.create(Into<Person>()) person.name = "John Smith" person.job = "Dentist" }, completion: { (result) -> Void in switch result { case .success: let people = CoreStore.fetchAll(From<Person>()) print (people!) case .failure(let error): print(error) } ```
Author
Owner

@martheli commented on GitHub (May 26, 2017):

I moved it inside the completion and the output is the same; no new values.

@martheli commented on GitHub (May 26, 2017): I moved it inside the completion and the output is the same; no new values.
Author
Owner

@JohnEstropia commented on GitHub (May 26, 2017):

@martheli Can you show your console log?

@JohnEstropia commented on GitHub (May 26, 2017): @martheli Can you show your console log?
Author
Owner

@martheli commented on GitHub (May 26, 2017):

Output is:

[<Person: 0x600000281680> (entity: Person; id: 0xd000000000040000 <x-coredata://091EEDA4-1652-4D5B-AEA0-110672DEBB1A/Person/p1> ; data: <fault>)]

@martheli commented on GitHub (May 26, 2017): Output is: `[<Person: 0x600000281680> (entity: Person; id: 0xd000000000040000 <x-coredata://091EEDA4-1652-4D5B-AEA0-110672DEBB1A/Person/p1> ; data: <fault>)]`
Author
Owner

@JohnEstropia commented on GitHub (May 26, 2017):

That looks like the array contents and the Person objects is in it, so I think there is no problem. You can try to print the person's name and job:

print(people![0].name)
print(people![0].job)
@JohnEstropia commented on GitHub (May 26, 2017): That looks like the array contents and the Person objects is in it, so I think there is no problem. You can try to print the person's name and job: ```swift print(people![0].name) print(people![0].job) ```
Author
Owner

@martheli commented on GitHub (May 26, 2017):

If people is an array, why can't I just print out the array like "print(people)" to list all the values stored in the entity?

@martheli commented on GitHub (May 26, 2017): If people is an array, why can't I just print out the array like "print(people)" to list all the values stored in the entity?
Author
Owner

@JohnEstropia commented on GitHub (May 26, 2017):

Because Core Data has something called faulting.
Basically all fetched objects's values would only be loaded in memory after the first value is accessed. You can see this behavior if you do this:

print(people!)
print(people![0].name)
print(people!)

You'll notice the last print will print the values because name was accessed on the second print

@JohnEstropia commented on GitHub (May 26, 2017): Because Core Data has something called [faulting](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/FaultingandUniquing.html). Basically all fetched objects's values would only be loaded in memory after the first value is accessed. You can see this behavior if you do this: ```swift print(people!) print(people![0].name) print(people!) ``` You'll notice the last `print` will print the values because `name` was accessed on the second `print`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#141