Is there a way to make a copy in memory of a persistent model ?
the goal is to make a compare.
I try to use backingData but when i update the main model the backingData is also updated.
Is this a bug or am not using the method properly ?
Best.
let model = Model()
let modelBackingData = Model(backingData: model.backingData)
Post
Replies
Boosts
Views
Activity
When you set your container "manually", how can we interact with the isAutosaveEnabled and isUndoEnabled parameters from the modelContainer ?
let schema:Schema = Schema([
Model.self
])
let configuration:ModelConfiguration = ModelConfiguration()
let container:ModelContainer = try ModelContainer(for:schema, configuration)
let context:ModelContext = ModelContext(container)
When navigating with the screen, the tab selection should be updated accordingly but it does not.
I try with the observable protocol or with the state property wrapper, same results...
import SwiftUI
import SwiftData
struct MainView: View {
@Environment(\.modelContext) private var db
@Environment(NavigationObservable.self) private var navigationObservable
// @State private var tab:String = "tab1"
var body: some View {
TabView(
selection:Binding(
get: { navigationObservable.tab },
set: { newValue in navigationObservable.tab = newValue }
)
) {
// TabView(
// selection:$tab
// ) {
Text("tab1")
.tag("tab1")
Text("tab2")
.tag("tab2")
}
.tabViewStyle(.page(indexDisplayMode:.never))
.onChange(of: navigationObservable.tab) {
print(navigationObservable.tab)
}
// .onChange(of: tab) {
// print(tab)
// }
}
}
I can't find a way to make this relationship, Anyone to tell me what am i doing wrong please ?
import SwiftUI
import SwiftData
@Model class Model1 {
@Attribute(.unique) var id:String
var date:Date
@Relationship(.cascade) var model2:[Model2]
init() {
self.id = UUID().uuidString
self.date = Date()
self.model2 = []
}
}
@Model class Model2 {
@Attribute(.unique) var id:String
var date:Date
@Relationship(inverse: \Model1.model2) var model1:Model1
init(
model1:Model1
) {
self.id = UUID().uuidString
self.date = Date()
self.model1 = model1
}
}
struct view1:View {
@Environment(\.modelContext) private var db
var body: some View {
Text("TapMe")
.onTapGesture {
let m1 = Model1()
db.insert(m1)
let m2 = Model2(model1: m1)
db.insert(m2)
}
}
}
Error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'm1' between objects in different contexts (source = <NSManagedObject: 0x280b253b0> (entity: Model2; id: 0x282896ca0 <x-coredata:///Model2/tCB971C15-AFE3-46A2-A1FB-E688E0EF2ABF207>; data: {
date = "2023-07-10 20:22:56 +0000";
id = "625B994D-24BB-4935-A438-8AA6522C0FE1";
m1 = nil;
}) , destination = <NSManagedObject: 0x280b250e0> (entity: Model1; id: 0x2828eaa40 <x-coredata:///Model1/tCB971C15-AFE3-46A2-A1FB-E688E0EF2ABF206>; data: {
date = "2023-07-10 20:22:56 +0000";
id = "BA3E6CF3-67EC-41CA-A150-E66E0D7226B2";
m2 = (
);
}))'
Hello,
We no longer can update manually the focusState.
@FocusState var focused:Bool
var body: some View {
Text("tapMe")
.onTapGesture { focused = true; print(focused) /** focused = false */ }
}
Best.
@Model class AModel {
@Attribute(.unique) var id:String
var date:Date
var b:[BModel]
init() {
self.id = UUID().uuidString
self.date = Date()
self.b = []
}
}
@Model class BModel {
@Attribute(.unique) var id:String
var date:Date
init() {
self.id = UUID().uuidString
self.date = Date()
}
}
struct MainView: View {
@Environment(\.modelContext) private var db
@State private var a:AModel = AModel()
var body: some View {
VStack {
}
.onAppear {
a.b.append(BModel())
print(a.b)
}
}
}
// CRASH :
@Model class AModel {
@Attribute(.unique) var id:String
var date:Date
var b:[BModel]
/**
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
{
get {
_$observationRegistrar.access(self, keyPath: \.b)
return self.getValue(for: \.b)
}
set {
_$observationRegistrar.withMutation(of: self, keyPath: \.b) {
self.setValue(for: \.b, to: newValue)
}
}
}
*/
init() {
self.id = UUID().uuidString
self.date = Date()
self.b = []
}
}
@Model class BModel {
@Attribute(.unique) var id:String
var date:Date
init() {
self.id = UUID().uuidString
self.date = Date()
}
}
Hello,
Since iOS16, I noticed a glitch when switching from tabN to tabN+1 or tabN-1 and reverse with complex embedded view.
Also the animation to switch tab programmatically are no longer working when I use an observableObject as a selection :
@EnvironmentObject var ViewModel: VM
TabView(selection: $ViewModel.tabRouter) {
View1()
.tag("first")
View2()
.tag("second")
View3()
.tag("third")
}
.animation(.default, value: ViewModel.tabRouter)
.tabViewStyle(.page(indexDisplayMode: .never))
.ignoresSafeArea(edges: .bottom)
On iOS15 everything works smoothly.
Best