Posts

Post not yet marked as solved
1 Replies
840 Views
I have been working on this for a while and haven't found a solution. I have a populated public core data database (used readonly in the app) that I need to pull from on launch. This ONLY works with SwiftUI via @FetchRequest or FetchRequest -- NSFetchResultsController, attempting to do it with persistentCloudKitContainer.viewContext.perform {} doesn't fetch (all do work after relaunching the app). The only thing that works is FetchRequest, but I need to do things in my model as soon as I get that data from CloudKit (create local data in response). How can I set, in my model, allItems to the wrappedValue property of the FetchRequest or @FetchRequest ? Since it is from cloudkit just setting the variable won't work, I need to use combine, but I can't wrap my head around how. If anyone has any suggestions -- I'd much appreciate the assistance. class MyViewModel: ObservableObject {     typealias PublisherType = PassthroughSubject<MyViewModel, Never>     let objectWillChange = ObservableObjectPublisher()     let objectDidChange = ObservableObjectPublisher() @Published var allItems: [Item] = [] struct MyView: View {     @FetchRequest var fetchRequest = FetchRequest<Item>(entity: Item.entity(), sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)])     var body: some View {            List(fetchRequest.wrappedValue) { (item:Item) in Text(item.name) } }
Posted
by rbenjamin.
Last updated
.
Post not yet marked as solved
0 Replies
884 Views
I'm having trouble implementing the TabView component on watchOS - wondering if anyone knows something I'm missing. It works fine on my iOS target, but on watchOS, a TabView with a List lets me tap one item, but returning to the main view and then tapping a second item won't result in the second view being loaded again. It works fine without the tabview of course. class Item: ObservableObject, Identifiable {     let name: String     init(name: String) {         self.name = name     } } struct ContentView: View {     @State var contents: [Item] = [Item(name: "Test"), Item(name: "Test 2")]     var body: some View {         TabView {         List(contents) { (item: Item) in             NavigationLink(item.name) {                 ItemView(item: item)             }         }         }     } } struct ItemView: View {     @State var item: Item     var body: some View {         VStack {             Text(item.name)         }     } }
Posted
by rbenjamin.
Last updated
.
Post marked as solved
2 Replies
2.0k Views
So I have this technique I've found I thought would work to seed a core data db with one I've made. My goal list to take a sqlite database (made with a rough companion app in swiftui), filled with content, that will be part of the application bundle. The goal is to copy that data into the user's core data store and allow them to favorite, make certain changes to the items loaded into the database. Is this technique I'm trying to use best for read-only databases or could I be implementing this wrong? Or is my best bet to copy over the sqlite once with FileManager? This is. my persistent container code: &#9;&#9;lazy var persistentContainer: NSPersistentContainer = { &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;let container = NSPersistentContainer(name: "UserStore") &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;if isFirstLaunch() { &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;let seededDataURL = Bundle.main.url(forResource: "PopulatedStore", withExtension: "sqlite") &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;if let seededDataURL = seededDataURL { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let persistentDescription = NSPersistentStoreDescription(url:seededDataURL) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;persistentDescription.shouldInferMappingModelAutomatically = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;persistentDescription.shouldMigrateStoreAutomatically = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;container.persistentStoreDescriptions = [persistentDescription] &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;container.loadPersistentStores { (storeDescription, error) in &#9;&#9;&#9;&#9;&#9;&#9;if let error = error { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print("error loading persistent store! \(error)") &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return container &#9;&#9;}() Thank you
Posted
by rbenjamin.
Last updated
.