JohnEstropia / CoreStore

Unleashing the real power of Core Data with the elegance and safety of Swift
MIT License
4k stars 254 forks source link

Idea: Asynchronous fetching #88

Open JohnEstropia opened 8 years ago

JohnEstropia commented 8 years ago

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
evanxlh commented 6 years ago

@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?

JohnEstropia commented 6 years ago

@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.

evanxlh commented 6 years ago

@JohnEstropia Thank you very much. I got it.