fix index
This commit is contained in:
parent
0ecb1dbb6a
commit
c90d0e51d1
@ -7,15 +7,29 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import SwiftData
|
import SwiftData
|
||||||
|
import UIKit
|
||||||
|
|
||||||
@Model
|
@Model
|
||||||
final class UserModel {
|
final class UserModel {
|
||||||
var userId: Int
|
var userId: String
|
||||||
var username: String
|
var username: String
|
||||||
|
|
||||||
init(userId: Int, username: String) {
|
init(userId: String, username: String) {
|
||||||
self.userId = userId
|
self.userId = userId
|
||||||
self.username = username
|
self.username = username
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func defaultUser() -> UserModel {
|
||||||
|
return UserModel(userId: getUserId(), username: "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取设备的唯一标识
|
||||||
|
private static func getUserId() -> String {
|
||||||
|
if let uuid = UIDevice.current.identifierForVendor?.uuidString {
|
||||||
|
return uuid.lowercased()
|
||||||
|
} else {
|
||||||
|
return UUID().uuidString.lowercased()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -110,7 +110,7 @@ final class IndexModel {
|
|||||||
|
|
||||||
struct IndexView: View {
|
struct IndexView: View {
|
||||||
@Environment(\.modelContext) private var modelContext
|
@Environment(\.modelContext) private var modelContext
|
||||||
@Query private var items: [Item]
|
//@Query private var items: [Item]
|
||||||
@Query private var userModel: [UserModel]
|
@Query private var userModel: [UserModel]
|
||||||
|
|
||||||
@State var indexModel = IndexModel()
|
@State var indexModel = IndexModel()
|
||||||
@ -229,22 +229,23 @@ struct IndexView: View {
|
|||||||
.ignoresSafeArea(edges: .bottom)
|
.ignoresSafeArea(edges: .bottom)
|
||||||
.task {
|
.task {
|
||||||
//await self.indexModel.loadData()
|
//await self.indexModel.loadData()
|
||||||
|
print(userModel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func addItem() {
|
private func addItem() {
|
||||||
withAnimation {
|
// withAnimation {
|
||||||
let newItem = Item(timestamp: Date())
|
// let newItem = Item(timestamp: Date())
|
||||||
modelContext.insert(newItem)
|
// modelContext.insert(newItem)
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
private func deleteItems(offsets: IndexSet) {
|
private func deleteItems(offsets: IndexSet) {
|
||||||
withAnimation {
|
// withAnimation {
|
||||||
for index in offsets {
|
// for index in offsets {
|
||||||
modelContext.delete(items[index])
|
// modelContext.delete(items[index])
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,10 +15,24 @@ struct dimensionhubApp: App {
|
|||||||
Item.self,
|
Item.self,
|
||||||
UserModel.self
|
UserModel.self
|
||||||
])
|
])
|
||||||
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
|
let modelConfiguration = ModelConfiguration(
|
||||||
|
schema: schema,
|
||||||
|
isStoredInMemoryOnly: false,
|
||||||
|
allowsSave: true
|
||||||
|
)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
return try ModelContainer(for: schema, configurations: [modelConfiguration])
|
let container = try ModelContainer(for: schema, configurations: [modelConfiguration])
|
||||||
|
|
||||||
|
// 如何用户不存在,则初始化一个用户
|
||||||
|
var descriptor = FetchDescriptor<UserModel>(sortBy: [])
|
||||||
|
descriptor.fetchLimit = 1
|
||||||
|
let users = try container.mainContext.fetch(descriptor)
|
||||||
|
if users.isEmpty {
|
||||||
|
container.mainContext.insert(UserModel.defaultUser())
|
||||||
|
}
|
||||||
|
|
||||||
|
return container
|
||||||
} catch {
|
} catch {
|
||||||
fatalError("Could not create ModelContainer: \(error)")
|
fatalError("Could not create ModelContainer: \(error)")
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user