CoreStore icon indicating copy to clipboard operation
CoreStore copied to clipboard

Idea: Asynchronous fetching

Open JohnEstropia opened this issue 7 years ago • 3 comments

Objective: allow a way to fetch objects in a way that can wait for completion of

  1. Store migrations (need to explicitly provide configuration in Into<Entity>("...") clause)
  2. Heavy queries

JohnEstropia avatar Aug 26 '16 04:08 JohnEstropia

@JohnEstropia Hi JohnEstropia, it's a great open source for core data. Does CoreStore only support fetch records on the main thread? If a large number of records, does it block the main thread?

evanxlh avatar Dec 27 '17 08:12 evanxlh

@evanxie-cn If you have very large fetches (e.g. image binary data, few thousand objects) you can always fetch from a background transaction and pass the info you need to the main thread. Here's an example:

dataStack.perform(
    asynchronous: { (transaction) -> [UIImage] in
        let people = transaction.fetchAll(From<Person>().where(/* ... */))
        return people.map { UIImage(data: $0.imageData)! }
    },
    completion: { (photos) in  /* ... */
        // use photos
    }
)

Depending on your confidence on your app architecture, you can also use unsafe transactions (dataStack.beginUnsafe()) in conjunction with your own DispatchQueues.

JohnEstropia avatar Dec 27 '17 12:12 JohnEstropia

@JohnEstropia Thank you very much. I got it.

evanxlh avatar Dec 28 '17 07:12 evanxlh