UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Document based CoreData app: What model class? UIManagedDocument? NSPersistentContainer?

Forums > SwiftUI

Hi everyone

My goal is to create a document based app (iOS and macOS) with a complex data model stored in CoreData. The model consists of various entities and relations.

The "basic" requirement I have: The user can choose a document from anywhere he likes (eg "Files" app on iOS; hard disk or cloud location on macOS) and starts working on the document. When the document is modified on another device, it must be ensured that the app detects this and automatically merge the changes (or shows possible conflicts).

Based on this requirements, I remembered that Apple used to have UIDocument and UIManagedDocument in UIKit land to help managing documents and ensuring they can update automatically based on changes in iCloud. UIDocument is used for simple data models which are fully kept in memory and which can be "snapshotted", put into file(s) and then restored again by loading from the file(s). This is very similar to to how ReferenceFileDocument works: the system calls snapshot(contentType:) to capture the document state and then asks for the "file representation" when it calls fileWrapper(snapshot:, configuration:)...

But looking at ReferenceFileDocument or FileDocument I don't see any discussion on how merging or conflict resolution is handled. Also there is still the issue of the missing CoreData functionality. I cannot keep my data model fully in memory.

So now I'm wondering is the time of UIDocument and UIManagedDocument something of the past? And if not, how would one make them fit with the SwiftUI-way of document based apps?

In the end I only wonder: how can I get a CoreData document based SwiftUI app? Should I try to make UIManagedDocument become ReferenceFileDocument compatible? (which I'm currently trying but I'm struggling) Or can I use NSPersistenContainer in some way?

Kind regards

Philipp

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.