Hello, have you found a solution for this? Thank You
Post
Replies
Boosts
Views
Activity
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentCloudKitContainer
init() {
container = NSPersistentCloudKitContainer(name: "MJ")
guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.Jad.MJ")?.appendingPathComponent("MJ.sqlite") else {
fatalError("Shared file container could not be created.")
}
let storeDescription = NSPersistentStoreDescription(url: fileContainer)
storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
storeDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "YOUR containerIdentifier")
container.persistentStoreDescriptions = [storeDescription]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
}
}
I tried to observe NSPersistentStoreRemoteChange like this:
.onReceive(NotificationCenter.default.publisher(for: .NSPersistentStoreRemoteChange)) { notification in
DispatchQueue.main.async {
let predicate = items.nsPredicate
items.nsPredicate = nil
items.nsPredicate = NSPredicate(value: true)
items.nsPredicate = predicate
}
}
but the operation that I execute in the closure isn't efficient and gives me an issue: Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates
import CoreData
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentContainer
init() {
container = NSPersistentContainer(name: "SiriShort")
guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.SiriShortcut2")?.appendingPathComponent("SiriShort.sqlite") else {
fatalError("Shared file container could not be created.")
}
let storeDescription = NSPersistentStoreDescription(url: fileContainer)
storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
container.persistentStoreDescriptions = [storeDescription]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
}
}
mistake
I just solved it, I forgot to add App Groups, however the app isn't refreshing, to see the new data I have to close it and reopen it, any clue on how to fix this issue, I will post my CoreData Stack
Ok Thank You
Why do you absolutely want to use predicate ?
Because I Want to use it in @FetchRequest
Does your actual code show the collapse button ?
Yes
Here is a very similar code
struct ContentView: View {
var body: some View {
NavigationView{
List{
NavigationLink {
Text("Hello")
} label: {
Text("Hello")
}
NavigationLink {
Text("World")
} label: {
Text("World")
}
}
.navigationTitle("Test")
}
}
}
What I want to really achieve is the sidebar that is used in the iPad Settings app, if you know how to do that, it would be perfect. Thank You very much
???
Is there another way? Because this .onRecevie(books.publisher) repeats too many times, I want it to execute one time?
It works, Thank You OOPer!
let date = Calendar.current.dateComponents(in: .current, from: Date()).date!
print(date)
Even if I do this it gives me the wrong date
Why?
And what if I wanted to write if Date() > Date() + 86400 for example, is it formatted?
You should probably have to recreate the database completely, just keeping existing records.
If you can explain better your solution it would be better for me
In any case, does this cause you problem or is this for understanding ?
It does not create me a direct problem, however I want to find a solution, because if you think about it, if you add and delete data every day for 100 days, even if there is no data (in the app) there will be a lot of MB occupied by the app in the iPhone Storage.
Thank You!