How to fetch objects in backgound? #145

Closed
opened 2025-12-29 15:25:30 +01:00 by adam · 2 comments
Owner

Originally created by @sidlatau on GitHub (Jun 7, 2017).

I am trying to fetch data in background, and use fetched data in main thread, but when I try to access managed objects properties app crashes. Problem for this I think is that background context is destroyed when transaction finishes. Is it possible to change managed objects context to main somehow (simmilar to transaction.edit(object) functionality)?

DataStack.beginAsynchronous { transaction in
               let data = transaction.fetchAll(
                 From<Object>(),
                 Where(...)
                ) ?? []
               transaction.commit { _ in
                     callback(data)
               }
}
Originally created by @sidlatau on GitHub (Jun 7, 2017). I am trying to fetch data in background, and use fetched data in main thread, but when I try to access managed objects properties app crashes. Problem for this I think is that background context is destroyed when transaction finishes. Is it possible to change managed objects context to main somehow (simmilar to ```transaction.edit(object)``` functionality)? ``` DataStack.beginAsynchronous { transaction in let data = transaction.fetchAll( From<Object>(), Where(...) ) ?? [] transaction.commit { _ in callback(data) } } ```
adam added the question label 2025-12-29 15:25:30 +01:00
adam closed this issue 2025-12-29 15:25:30 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jun 7, 2017):

Yes, you need to fetch it again from your datastack:

callback(DataStack.fetchExisting(data)!)
@JohnEstropia commented on GitHub (Jun 7, 2017): Yes, you need to fetch it again from your datastack: ```swift callback(DataStack.fetchExisting(data)!) ```
Author
Owner

@sidlatau commented on GitHub (Jun 7, 2017):

That's exactly what I needed. Thanks for quick response.

@sidlatau commented on GitHub (Jun 7, 2017): That's exactly what I needed. Thanks for quick response.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#145