Change local data doesn't updated when fetchingOne object from core store #297

Open
opened 2025-12-29 15:28:20 +01:00 by adam · 6 comments
Owner

Originally created by @pajtimid on GitHub (Dec 6, 2019).

I'm changing local data and they aren't updating. I have to kill app and after opening again they are updated.

dataStack.perform(
           asynchronous: { (transaction) -> Void in
               let newOffline = transaction.create(Into<CDOffline>())
               newOffline.setValues(data: data)
               
               let stored = transaction.fetchOne(
                   From<CDUserData>()
                       .where(\.id == Int32(id))
               )
       
               if let local= stored {
                   local.used = newOffline.used
               }
       },completion: { (result) -> Void in
           completion()
       })

I'm using async callbacks and after completion needs to be updated.

 func userData(id: Int) -> UserData! {
       let storedUserData = self.dataStack.fetchOne(
           From<CDUserData >()
               .where(\.id == Int32(id))
       )
       return storedUserData.map{ UserData(fromCoreData: $0) }
   }

the last func doesnt getting updated local datas.

P.S. In SquelLite browser shows changed data

Originally created by @pajtimid on GitHub (Dec 6, 2019). I'm changing local data and they aren't updating. I have to kill app and after opening again they are updated. ``` dataStack.perform( asynchronous: { (transaction) -> Void in let newOffline = transaction.create(Into<CDOffline>()) newOffline.setValues(data: data) let stored = transaction.fetchOne( From<CDUserData>() .where(\.id == Int32(id)) ) if let local= stored { local.used = newOffline.used } },completion: { (result) -> Void in completion() }) ``` I'm using async callbacks and after completion needs to be updated. ``` func userData(id: Int) -> UserData! { let storedUserData = self.dataStack.fetchOne( From<CDUserData >() .where(\.id == Int32(id)) ) return storedUserData.map{ UserData(fromCoreData: $0) } } ``` the last func doesnt getting updated local datas. P.S. In SquelLite browser shows changed data
adam added the question label 2025-12-29 15:28:20 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Dec 7, 2019):

If there aren't any ListMonitors, ObjectMonitors, or ObjectPublishers attached to this object it will never be updated on its own. My suggestion here is to use ObjectPublisher as it is the most lightweight. (See https://github.com/JohnEstropia/CoreStore#observe-a-single-objects-updates )

Once any of the monitor/publisher types are attached to that object, you don't need to call anything else to keep that object updated. (Observation will allow you to react to those changes though)

@JohnEstropia commented on GitHub (Dec 7, 2019): If there aren't any `ListMonitor`s, `ObjectMonitor`s, or `ObjectPublisher`s attached to this object it will never be updated on its own. My suggestion here is to use `ObjectPublisher` as it is the most lightweight. (See https://github.com/JohnEstropia/CoreStore#observe-a-single-objects-updates ) Once any of the monitor/publisher types are attached to that object, you don't need to call anything else to keep that object updated. (Observation will allow you to react to those changes though)
Author
Owner

@pajtimid commented on GitHub (Dec 9, 2019):

I'm not understanding clearly the way that I should implement this ObjectPublisher. Can you help me on this, where to put publisher?

@pajtimid commented on GitHub (Dec 9, 2019): I'm not understanding clearly the way that I should implement this ObjectPublisher. Can you help me on this, where to put publisher?
Author
Owner

@AlwaysBee commented on GitHub (Dec 15, 2019):

Please make sure that the "dataStack" is the SAME one, otherwise your update would not works in different dataStack, unless you relaunch the app

@AlwaysBee commented on GitHub (Dec 15, 2019): Please make sure that the "dataStack" is the SAME one, otherwise your update would not works in different dataStack, unless you relaunch the app
Author
Owner

@JohnEstropia commented on GitHub (Dec 16, 2019):

Please make sure that the "dataStack" is the SAME one

Ah yes, there is this too

@JohnEstropia commented on GitHub (Dec 16, 2019): > Please make sure that the "dataStack" is the SAME one Ah yes, there is this too
Author
Owner

@JohnEstropia commented on GitHub (Dec 16, 2019):

I'm not understanding clearly the way that I should implement this ObjectPublisher. Can you help me on this, where to put publisher?

@pajtimid As @AlwaysBee mentioned, it's possible you are using a default-initialized stack. This is a much more common mistake so please check first that:

  1. The DataStack you created and assigned to self.dataStack is not getting accessed anywhere else before your addStorage(...) executes its completion block.
  2. The DataStack you are calling perform(asynchronous:completion:) on is still the same instance as (1)
  3. The DataStack you are calling fetchOne(_:) on is still the same instance as (1) and (2)
@JohnEstropia commented on GitHub (Dec 16, 2019): > I'm not understanding clearly the way that I should implement this ObjectPublisher. Can you help me on this, where to put publisher? @pajtimid As @AlwaysBee mentioned, it's possible you are using a default-initialized stack. This is a much more common mistake so please check first that: 1. The `DataStack` you created and assigned to `self.dataStack` is not getting accessed anywhere else before your `addStorage(...)` executes its `completion` block. 2. The `DataStack` you are calling `perform(asynchronous:completion:)` on is still the same instance as (1) 3. The `DataStack` you are calling `fetchOne(_:)` on is still the same instance as (1) and (2)
Author
Owner

@pajtimid commented on GitHub (Dec 20, 2019):

I solved it. My problem was on: one-to-many relationship.
My locations variable is Set<Locations> type

` for location in locations {
                            let newDataLocations = transaction.create(Into<CDDataLocations>())
                            newDataLocations.setValues(dataLocations: location)
                            newData.locations.insert(newDataLocation)
                        }`

I removed this line
newData.locations.insert(newDataLocation)
and updated well.

I don't know why this is happening!

@pajtimid commented on GitHub (Dec 20, 2019): I solved it. My problem was on: one-to-many relationship. My` locations` variable is `Set<Locations> `type ``` ` for location in locations { let newDataLocations = transaction.create(Into<CDDataLocations>()) newDataLocations.setValues(dataLocations: location) newData.locations.insert(newDataLocation) }` ``` I removed this line `newData.locations.insert(newDataLocation)` and updated well. I don't know why this is happening!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#297