Hi,
I am inserting two models where the "unique" attribute is the same. I was under the impression, that this should result in an upsert and not two inserts of the model, but that is not the case.
See the test coding below for what I am doing (it is self contained, so if you want to try it out, just copy it into a test target). The last #expect statement fails because of the two inserts. Not sure if this is a bug (Xcode 16 beta 2 on Sonoma running an iOS 18 simulator) or if I am missing something here...
// MARK: - UniqueItem -
@Model
final class UniqueItem {
#Unique<UniqueItem>([\.no])
var timestamp = Date()
var title: String
var changed = false
var no: Int
init(title: String, no: Int) {
self.title = title
self.no = no
}
}
// MARK: - InsertTests -
@Suite("Insert Tests", .serialized)
struct InsertTests {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
UniqueItem.self,
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
@Test("Test unique.")
@MainActor func upsertAndModify() async throws {
let ctx = sharedModelContainer.mainContext
try ctx.delete(model: UniqueItem.self)
let item = UniqueItem(title: "Item \(1)", no: 0)
ctx.insert(item)
let allFD = FetchDescriptor<UniqueItem>()
let count = try ctx.fetchCount(allFD)
#expect(count == 1)
let updatedItem = UniqueItem(title: "Item \(1)", no: 0)
updatedItem.changed = true
ctx.insert(updatedItem)
// we should still have only 1 item because of the unique constraint
let allCount = try ctx.fetchCount(allFD)
#expect(allCount == 1)
}
}
iCloud & Data
RSS for tagLearn how to integrate your app with iCloud and data frameworks for effective data storage
Post
Replies
Boosts
Views
Activity
In my recent endeavor, I aimed to introduce new Fetch Index Elements to the Core Data model of my iOS application. To achieve this, I followed a process of lightweight migration, detailed as follows:
Navigate to Editor > Add Model Version to create a new version of the data model.
Name the new version with a sequential identifier (e.g., MyAppModelV3.xcdatamodel) based on the naming convention of previous models.
Select the newly created version, MyAppModelV3.xcdatamodel, as the active model.
Mark this new version as the "Current" model in the Xcode properties panel on the right.
In the new version of the model, MyAppModelV3.xcdatamodel, and add the new Fetch Index Elements there. Also, insert "v3" in the Versioning Hash Modifier field of affected entity, to indicate this modification.
Upon reflection, I realized that creating a new version of the xcdatamodel might not have been necessary for this particular case. However, it appears to have caused no adverse effects on the application's functionality.
During testing, I executed the application in a simulated environment, initially running an older version of the app to inspect the database content with SQLite DB Browser. I then upgraded to the latest app version to verify that the migration was successfully completed without causing any crashes. Throughout this testing phase, I employed the -com.apple.CoreData.MigrationDebug 1 flag to monitor all SQL operations, ensuring that indexes were appropriately dropped and recreated for the affected entity.
Following thorough testing, I deployed the update to production. The majority of users were able to upgrade to the new app version seamlessly. However, a small fraction reported crashes at startup, indicated by the following error message:
Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={NSUnderlyingError=0x2820ad3e0 {Error Domain=NSCocoaErrorDomain Code=134100 "The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store." UserInfo={metadata={ NSPersistenceFrameworkVersion = 1338; NSStoreModelVersionChecksumKey = "qcPf6+DfpsPrDQ3j1EVXcBIrFe1O0R6IKd30sJf4IrI="; NSStoreModelVersionHashes = { NSAttachment = {length = 32, ...
Strangely, the only way I could replicate this issue in the simulator was by running the latest version of the app followed by reverting to an older version, a scenario unlikely to occur in a real-world setting. This raises the question: How could this situation arise with actual users, considering they would typically move from an old to a new version rather than the reverse?
I am reaching out to the community for insights or advice on this matter. Has anyone else encountered a similar problem during the Core Data migration process? How did you resolve it?
I have added core data to my project and below is the code block of how it's section in pbxproj file looks like.
However whenever I make any changes to the project i.e. changing build version or adding a deleting new files to the project, I have noticed "name = "sample-app.xcdatamodeld";" gets deleted automatically from the project file and I have to manually reverse that change.
Am I missing any setting for core data or is it a bug somewhere in XCode or Core Data? I am using XCode 15.
/* Begin XCVersionGroup section */
4683EC5B2C10F8B800A5081B /* sample-app.xcdatamodeld */ = {
isa = XCVersionGroup;
children = (
4683EC5C2C10F8B800A5081B /* sample-app.xcdatamodel */,
);
currentVersion = 4683EC5C2C10F8B800A5081B /* sample-app.xcdatamodel */;
name = "sample-app.xcdatamodeld";
path = "sample-app.xcdatamodeld";
sourceTree = "<group>";
versionGroupType = wrapper.xcdatamodel;
};
/* End XCVersionGroup section */
Hi guys. Can someone please confirm this bug so I report it? The issue is that SwiftData relationships don't update the views in some specific situations on devices running iOS 18 Beta. One clear example is with CloudKit. I created a small example for testing. The following code creates two @models, one to store bands and another to store their records. The following code works with no issues. (You need to connect to a CloudKit container and test it on two devices)
import SwiftUI
import SwiftData
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query private var records: [Record]
var body: some View {
NavigationStack {
List(records) { record in
VStack(alignment: .leading) {
Text(record.title)
Text(record.band?.name ?? "Undefined")
}
}
.toolbar {
ToolbarItem {
Button("Add Record") {
let randomNumber = Int.random(in: 1...100)
let newBand = Band(name: "New Band \(randomNumber)", records: nil)
modelContext.insert(newBand)
let newRecord = Record(title: "New Record \(randomNumber)", band: newBand)
modelContext.insert(newRecord)
}
}
}
}
}
}
@Model
final class Record {
var title: String = ""
var band: Band?
init(title: String, band: Band?) {
self.title = title
self.band = band
}
}
@Model
final class Band {
var name: String = ""
var records: [Record]?
init(name: String, records: [Record]?) {
self.name = name
self.records = records
}
}
This view includes a button at the top to add a new record associated with a new band. The data appears on both devices, but if you include more views inside the List, the views on the second device are not updated to show the values of the relationships. For example, if you extract the row to a separate view, the second device shows the relationships as "Undefined". You can try the following code.
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query private var records: [Record]
var body: some View {
NavigationStack {
List {
ForEach(records) { record in
RecordRow(record: record)
}
}
.toolbar {
ToolbarItem {
Button("Add Record") {
let randomNumber = Int.random(in: 1...100)
let newBand = Band(name: "New Band \(randomNumber)", records: nil)
modelContext.insert(newBand)
let newRecord = Record(title: "New Record \(randomNumber)", band: newBand)
modelContext.insert(newRecord)
}
}
}
}
}
}
struct RecordRow: View {
let record: Record
var body: some View {
VStack(alignment: .leading) {
Text(record.title)
Text(record.band?.name ?? "Undefined")
}
}
}
Here I use a ForEach loop and move the row to a separate view. Now on the second device the relationships are nil, so the row shows the text "Undefined" instead of the name of the band.
I attached an image from my iPad. I inserted all the information on my iPhone. The first three rows were inserted with the first view. But the last two rows were inserted after I extracted the rows to a separate view. Here you can see that the relationships are nil and therefore shown as "Undefined". The views are not updated to show the real value of the relationship.
This example shows the issue with CloudKit, but this also happens locally in some situations. The system doesn't detect updates in relationships and therefore doesn't refresh the views.
Please, let me know if you can reproduce the issue. I'm using Mac Sequoia 15.1, and two devices with iOS 18.0.
I'm encountering an issue when trying to store a SIMD3<Float> in a SwiftData model. Since SIMD3<Float> already conforms to Codable, I expected it to work. However, attempting to store a single SIMD3<Float> crashes with the following error:
Fatal error: Unexpected property within Persisted Struct/Enum: Builtin.Vec4xFPIEEE32
Interestingly, storing an array of vectors, [SIMD3<Float>], works perfectly fine. The issue only arises when trying to store a single SIMD3<Float>.
I’m not looking for a workaround (I can break the vector into individual floats in a custom codable struct to get by) , but I’d like to understand why storing a codable SIMD3<Float> in SwiftData results in this crash. Is this a limitation of SwiftData, or is there something I’m missing about how vectors are handled?
Any insights would be greatly appreciated!
Consider a sample SwiftData project
var body: some View {
List(recipes) { recipe in
NavigationLink(recipe.name, destination: RecipeView(recipe))
}
}
For SwiftUI views that uses SwiftData it's very straight forward.
However, if I need to read/write to SwiftData while in the background (let's say after a network call), the app crashes.
Imagine a simple workflow where
(1) user selects some model objects (via SwiftData modelContext)
(2) now I need to pass these objects to some API server
(3) in a background thread somewhere I need to use the modelActor but it now cannot read/write to the original model objects without risk of crashing
So instead, the only way I thought of is to create a separate modelContext for background processing. There I passed down the container from the main app, and creates a new ModelActor that takes in the container and owns a new modelContext of its own.
This works, without crash. However it introduces many side effects:
(1) read/write from this modelActor seems to trigger view changes for SwiftData's modelContext. But not vice versa.
(2) model objects fetched from one context cannot be used in another context.
(3) changes made in the actor also doesn’t not automatically sync with icloud but changes in SwiftData’s modelContext do.
So I guess the bigger question here is, what is the proper usage of SwiftData in a background thread?
My app has been in the App Store a few months. In that time I've added a few updates to my SwiftData schema using a MigrationPlan, and things were seemingly going ok. But then I decided to add CloudKit syncing. I needed to modify my models to be compatible. So, I added another migration stage for it, changed the properties as needed (making things optional or adding default values, etc.). In my tests, everything seemed to work smoothly updating from the previous version to the new version with CloudKit. So I released it to my users. But, that's when I started to see the crashes and error reports come in. I think I've narrowed it down to when users update from older versions of the app. I was finally able to reproduce this on my end, and Core Data is throwing an error when loading the ModelContainer saying "CloudKit integration requires that all attributes be optional, or have a default value set." Even though I did this in the latest schema. It’s like it’s trying to load CloudKit before performing the schema migration, and since it can’t, it just fails and won’t load anything. I’m kinda at a loss how to recover from this for these users other than tell them to delete their app and restart, but obviously they’ll lose their data that way. The only other idea I have is to setup some older builds on TestFlight and direct them to update to those first, then update to the newest production version and hope that solves it. Any other ideas? And what can I do to prevent this for future users who maybe reinstall the app from an older version too? There's nothing special about my code for loading the ModelContainer. Just a basic:
let container = try ModelContainer(
for: Foo.self, Bar.self,
migrationPlan: SchemaMigration.self,
configurations: ModelConfiguration(cloudKitDatabase: .automatic)
)
In iOS 18 RC, and the iOS 18 simulator shipped with Xcode 16.0 RC, there is a regression where ModelContexts on the same ModelContainer do not sync changes. A minimal example is below, but briefly: create an object in context1. Retrieve and update that object in context2, then save context2. The changes cannot be found in context1 in iOS 18 RC, but can in iOS 17 and earlier betas of iOS 18.
I've submitted this as FB15092827 but am posting here for visibility to others. I'm going to have to scramble to see if I can mitigate this in our impacted app before iOS 18 launches. It's affecting us when doing background inserts in a ModelActor to populate our app UI, but you can see below the effects are seen even on the same thread in a very simple two-context example.
@Test("updates sync between contexts") func crossContextSync() async throws {
// overview:
// create an employee in context 1
// update the employee in context 2
// check that the update is available in context 1
let context1 = ModelContext(demoAppContainer)
let context2 = ModelContext(demoAppContainer)
// create an employee in context 1
let newEmployee = Employee(salary: 0)
context1.insert(newEmployee)
try context1.save()
#expect(newEmployee.salary == 0, "Created with salary 0")
// update the employee in context 2
let employeeID = newEmployee.uuid
let predicate: Predicate<Employee> = #Predicate<Employee> { employee in
employee.uuid == employeeID
}
let fetchedEmployee = try #require(try? context2.fetch(FetchDescriptor<Employee>(predicate: predicate)).first)
#expect(fetchedEmployee.uuid == newEmployee.uuid, "We got the correct employee in the new context")
let updatedSalary = 1
fetchedEmployee.salary = updatedSalary
try context2.save()
// FAILURE IS HERE. This passes in earlier iOS betas and in iOS 17.X
#expect(newEmployee.salary == updatedSalary, "Salary was update in context 1")
// Create a new modelContext on the same container, since the container does have the changes in it.
// By creating this new context we can get updated data and the test below passes in all iOS versions tested. This may be a mitigation path but creating new contexts any time you need to access data is painful.
let context3 = ModelContext(demoAppContainer)
let fetchedEmployeeIn3 = try #require(try? context3.fetch(FetchDescriptor<Employee>(predicate: predicate)).first)
#expect(fetchedEmployeeIn3.uuid == newEmployee.uuid, "We got the correct employee in the new context3")
#expect(fetchedEmployeeIn3.salary == updatedSalary, "Salary was update in context 1")
}
Code below if you want to build a working example, but the test above is very simple
let demoAppContainer = try! ModelContainer(for: Employee.self)
@main
struct ModelContextsNotSyncedToContainerApp: App {
init() {
}
var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(demoAppContainer)
}
}
}
@Model
final class Employee {
var uuid: UUID = UUID()
var salary: Int
init(salary: Int = 0) {
self.salary = salary
}
}
I have a type which represents an ID. This ID comes from a backend as an untyped string. The Swift type looks something like this:
struct TypedID: Codable {
init(_ string: String) {
stringValue = string
}
init(from decoder: any Decoder) throws {
stringValue = try decoder.singleValueContainer().decode(String.self)
}
func encode(to encoder: any Encoder) throws {
try stringValue.encode(to: encoder)
}
let stringValue: String
}
If I use this type in a SwiftData @Model, or even as a property of another Codeable struct which is contained in the @Model, SwiftData fails to create instances of my model and emits the following error:
CoreData: error: CoreData: error: Row (pk = #) for entity '<@Model type>' is missing mandatory text data for property 'stringValue'
This issue goes away if I remove the custom implementation of init(from:) and encode(to:), but doing so makes the coded representation of my type an object instead of a string, which is not what I want.
/// JSON without custom implementations:
{
"stringValue": "<the ID>"
}
/// JSON with custom implementations:
"<the ID>"
Is there anything I can do to be able to serialize a Codable type with a custom coding implementation like this to a SwiftData model?
I am seeking guidance on handling field-level schema changes in CKSyncEngine, specifically when introducing new fields in a subsequent app version.
The current CKSyncEngine documentation (https://developer.apple.com/documentation/cloudkit/cksyncengine) and the GitHub sample (https://github.com/apple/sample-cloudkit-sync-engine) provide clear instructions for managing changes to existing CKRecords. However, I am uncertain about the best approach for handling newly added fields in a new version of my app.
For example:
In version 1 of my app, I have a CKRecord named User with a field called name.
In version 2, I plan to add a new field, phone_number, to the User record. When version 1 (e.g., installed on a user's iPad) and version 2 (e.g., installed on the same user's iPhone) sync using CKSyncEngine, version 1 is unaware of the phone_number field.
My concern is how to ensure version 1 handles this scenario gracefully without blocking other sync events.
Additionally, when version 1 on the iPad is later updated to version 2, there will be no new sync events unless the "phone_number" field is modified again. This could result in the "phone_number" field never being synced to the iPad.
Could you please advise on the best practices for handling such cases to ensure seamless synchronization across different app versions?
Thank you for your assistance.
Starting point
I have an app that is in production that has a single entity called CDShift. This is the class:
@Model
final class CDShift {
var identifier: UUID = UUID()
var date: Date = Date()
...
}
This is how this model is written in the current version.
Where I need to go
Now, I'm updating the app and I have to do some modifications, that are:
add a new entity, called DayPlan
add the relationship between DayPlan and CDShift
What I did is this:
enum SchemaV1: VersionedSchema {
static var versionIdentifier = Schema.Version(1, 0, 0)
static var models: [any PersistentModel.Type] {
[CDShift.self]
}
@Model
final class CDShift {
var identifier: UUID = UUID()
var date: Date = Date()
}
}
To encapsulate the current CDShift in a version 1 of the schema. Then I created the version 2:
enum SchemaV2: VersionedSchema {
static var versionIdentifier = Schema.Version(2, 0, 0)
static var models: [any PersistentModel.Type] {
[CDShift.self, DayPlan.self]
}
@Model
final class DayPlan {
var identifier: UUID = UUID()
var date: Date = Date()
@Relationship(inverse: \CDShift.dayPlan) var shifts: [CDShift]? = []
}
@Model
final class CDShift {
var identifier: UUID = UUID()
var date: Date = Date()
var dayPlan: DayPlan? = nil
}
}
The migration plan
Finally, I created the migration plan:
enum MigrationPlan: SchemaMigrationPlan {
static var schemas: [any VersionedSchema.Type] {
[SchemaV1.self, SchemaV2.self]
}
static let migrateV1toV2 = MigrationStage.custom(
fromVersion: SchemaV1.self,
toVersion: SchemaV2.self) { context in
// willMigrate, only access to old models
} didMigrate: { context in
// didMigrate, only access to new models
let shifts = try context.fetch(FetchDescriptor<SchemaV2.CDShift>())
for shift in shifts {
let dayPlan = DayPlan(date: shift.date)
dayPlan.shifts?.append(shift)
context.insert(dayPlan)
}
}
static var stages: [MigrationStage] {
print("MigrationPlan | stages called")
return [migrateV1toV2]
}
}
The ModelContainer
Last, but not least, how the model container is created in the App:
struct MyApp: App {
private let container: ModelContainer
init() {
container = ModelContainer.appContainer
}
var body: some Scene {
WindowGroup {
...
}
.modelContainer(container)
}
}
This is the extension of ModelContainer:
extension ModelContainer {
static var appContainer: ModelContainer {
let schema = Schema([
CDShift.self,
DayPlan.self
])
let modelConfiguration = ModelConfiguration(
schema: schema,
isStoredInMemoryOnly: Ecosystem.current.isPreview,
groupContainer: .identifier(Ecosystem.current.appGroupIdentifier)
)
do {
// let container = try ModelContainer(for: schema, configurations: modelConfiguration)
let container = try ModelContainer(for: schema, migrationPlan: MigrationPlan.self, configurations: modelConfiguration)
AMLogger.verbose("SwiftData path: \(modelConfiguration.url.path)")
return container
} catch (let error) {
fatalError("Could not create ModelContainer: \(error)")
}
}
}
The error
This has always worked perfectly until the migration. It crashes on the fatalError line, this is the error:
Unable to find a configuration named 'default' in the specified managed object model.
Notes
It seems that the version of the store is never updated to 2, but it keeps staying on 1. I tried also using the lightweight migration, no crash, it seems it recognizes the new entity, but the store version is always 1.
iCloud is enabled
I thought that the context used in the custom migration blocks is not the "right" one that I use when I create my container
If I use the lightweight migration, everything seems to work fine, but I have to manually do the association between the DayPlan and the CDShift objects
Do you have an idea on how to help in this case?
I am unsure the correct way to model my data.
I have a swift data object and then some referenced objects. Is there ever a reason those should just be structs or should they always be swift data objects themselves?
for example right now this is what I'm doing:
@Model
final class Exam {
var timestamp: Date
@Attribute(.unique) var examID: UUID
var title: String
var questions: [Question]
...
}
and Question is
struct Question: Codable, Identifiable {
var id: UUID
var number: Int
var points: Int
var prompt: String
var answer: String
}
is there any problem with this or should I not be using a Struct for Question and instead use another Swift Data object with @Relationship ?
I thought since its a simple object just using a struct would be fine, however...
when I create a new Question object, it seems to create SwiftUI retain cycles with the warning
=== AttributeGraph: cycle detected through attribute 633984 ===
in the terminal
for example,
Button("Add Question", systemImage: "questionmark.diamond") {
let newQuestion = Question(id: UUID(), number: exam.questions.count+1, points: 1, prompt: "", answer: "", type: .multipleChoice)
exam.questions.append(newQuestion)
}
So, is it ok to mix structs with swift data objects or is it not best practice?
And is this causing the SwiftUI retain cycles or are the issues unrelated?
I'm still getting started with SwiftData and having trouble understanding how to persist a model with a relationship in a container. I've tried to distill the example below to be pretty simple.
Here are some example model definitions. I would like to be able to define a n Item. That Item can have SubItems related to it in a one-to-many fashion. Each SubItem is required to be attached to a parent Item.
final class Item {
var name: String
var subitems: [Item]?
init(
name: String,
subitems: [SubItem]? = nil
) {
self.name = name
}
}
@Model
final class SubItem {
var name: String
init(name: String) {
self.name = name
}
}
In my app I am then defining a preview container with some pre-saved data already.
Item(
name: "item1",
subitems: [
SubItem(name: "subItemA"),
SubItem(name: "subItemB")
]
),
Item(
name: "item2",
subitems: [
SubItem(name: "subItemC"),
SubItem(name: "subItemD")
]
)
]
@MainActor
let PreviewContainer: ModelContainer = {
do {
let schema = Schema([
Item.self,
SubItem.self,
])
let container = try ModelContainer(
for: schema, configurations: ModelConfiguration(isStoredInMemoryOnly: true)
)
for item in PreviewItems {
container.mainContext.insert(item)
for subItem in item.subitems! {
container.mainContext.insert(subItem)
}
}
return container
} catch {
fatalError("Failed to create container")
}
}()
My app is then defined as follows...
struct SwiftDataTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(PreviewContainer)
}
}
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query private var items: [Item]
var body: some View {
HStack {
VStack {
Text(items[0].name)
Text(String(items[0].subitems!.count))
// Text(items[0].subitems![0].name)
// Text(items[0].subitems![1].name)
}
Spacer()
VStack {
Text(items[1].name)
Text(String(items[1].subitems!.count))
// Text(items[0].subitems![0].name)
// Text(items[0].subitems![1].name)
}
}
.padding(100)
}
}
#Preview {
ContentView()
.modelContainer(PreviewContainer)
}
The preview loads without an issue, but if I uncomment the lines that access the SubItems it crashes. In the preview I can also see that each Item has 0 SubItems related to it. For some reason, the model container is not actually storing the `SubItem even though they are defined in PreviewItems.
Some things I've tried
Explicitly adding the relationship
Adding a Item property in SubItem to link back to it's parent
In the PreviewContainer definition, manually insert the SubItems as well as the parent Items
Any help is appreciated, thanks
Hi,
I understand how to make one to many relationship in SwiftData and how to show the child records, like all cities of a country. But how to navigate and show the parent record from a child record, Like I want to show a country of a city ?
like country.cities show all cities of a country, will cities.country work to show the country ? like cities.country.name ?
Kind Regards
Hello,
i have a route with many points
for routes:
@Model
public class Route(){
public id: UUID = UUID()
var name: String
var desc: String
var points: [Point] = []
}
@Model
public class Point(){
public id: UUID = UUID()
var speed: double
var route : Route
}
when I like to add point.
route.point.append(point)
I get all ways this error:
Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1cc1698ec)
my Xcode version 15.3
Hi,
I saw many examples on how to insert one model object at a time in SwiftData using modelContext.insert() , but can that command insert an array of objects in one batch as well ?
--
Kind Regards
Most of my CloudKit data has been missing from my container for over a week. No response from Apple support. I am very worried it may be unrecoverable.my apps can not access the data and the dashboard shows a huge amount of files and data in other files missing. These are needed to operate our chiropractic clinic and contain patient and transaction history. This might be a disaster for us.
Hi all,
I'm getting a strange SwiftData error at runtime in my voice recorder app. Whenever I attempt to generate and cache a samples array so that my app can visualize the waveform of the audio, the app crashes and the following pops up in Xcode:
{
@storageRestrictions(accesses: _$backingData, initializes: _samples)
init(initialValue) {
_$backingData.setValue(forKey: \.samples, to: initialValue)
_samples = _SwiftDataNoType()
}
get {
_$observationRegistrar.access(self, keyPath: \.samples)
return self.getValue(forKey: \.samples)
}
set {
_$observationRegistrar.withMutation(of: self, keyPath: \.samples) {
self.setValue(forKey: \.samples, to: newValue)
}
}
}
With an execution breakpoint on the line _$observationRegistrar.withMutation(of: self, keyPath: \.samples).
Here is my model class:
import Foundation
import SwiftData
@Model final class Recording {
var id : UUID?
var name : String?
var date : Date?
var samples : [Float]? = nil
init(name: String) {
self.id = UUID()
self.name = name
self.date = Date.now
}
}
And here is where the samples are being generated (sorry for the long code):
private func processSamples(from audioFile: AVAudioFile) async throws -> [Float] {
let sampleCount = 128
let frameCount = Int(audioFile.length)
let samplesPerSegment = frameCount / sampleCount
let buffer = try createAudioBuffer(for: audioFile, frameCapacity: AVAudioFrameCount(samplesPerSegment))
let channelCount = Int(buffer.format.channelCount)
let audioData = try readAudioData(from: audioFile, into: buffer, sampleCount: sampleCount, samplesPerSegment: samplesPerSegment, channelCount: channelCount)
let processedResults = try await processAudioSegments(audioData: audioData, sampleCount: sampleCount, samplesPerSegment: samplesPerSegment, channelCount: channelCount)
var samples = createSamplesArray(from: processedResults, sampleCount: sampleCount)
samples = applyNoiseFloor(to: samples, noiseFloor: 0.01)
samples = normalizeSamples(samples)
return samples
}
private func createAudioBuffer(for audioFile: AVAudioFile, frameCapacity: AVAudioFrameCount) throws -> AVAudioPCMBuffer {
guard let buffer = AVAudioPCMBuffer(pcmFormat: audioFile.processingFormat, frameCapacity: frameCapacity) else {
throw Errors.AudioProcessingError
}
return buffer
}
private func readAudioData(from audioFile: AVAudioFile, into buffer: AVAudioPCMBuffer, sampleCount: Int, samplesPerSegment: Int, channelCount: Int) throws -> [[Float]] {
var audioData = [[Float]](repeating: [Float](repeating: 0, count: samplesPerSegment * channelCount), count: sampleCount)
for segment in 0..<sampleCount {
let segmentStart = AVAudioFramePosition(segment * samplesPerSegment)
audioFile.framePosition = segmentStart
try audioFile.read(into: buffer)
if let channelData = buffer.floatChannelData {
let dataCount = samplesPerSegment * channelCount
audioData[segment] = Array(UnsafeBufferPointer(start: channelData[0], count: dataCount))
}
}
return audioData
}
private func processAudioSegments(audioData: [[Float]], sampleCount: Int, samplesPerSegment: Int, channelCount: Int) async throws -> [(Int, Float)] {
try await withThrowingTaskGroup(of: (Int, Float).self) { taskGroup in
for segment in 0..<sampleCount {
let segmentData = audioData[segment]
taskGroup.addTask {
var rms: Float = 0
vDSP_rmsqv(segmentData, 1, &rms, vDSP_Length(samplesPerSegment * channelCount))
return (segment, rms)
}
}
var results = [(Int, Float)]()
for try await result in taskGroup {
results.append(result)
}
return results
}
}
private func createSamplesArray(from processedResults: [(Int, Float)], sampleCount: Int) -> [Float] {
var samples = [Float](repeating: 0, count: sampleCount)
vDSP_vfill([0], &samples, 1, vDSP_Length(sampleCount))
for (segment, rms) in processedResults {
samples[segment] = rms
}
return samples
}
private func applyNoiseFloor(to samples: [Float], noiseFloor: Float) -> [Float] {
var result = samples
let noiseFloorArray = [Float](repeating: noiseFloor, count: samples.count)
vDSP_vsub(noiseFloorArray, 1, samples, 1, &result, 1, vDSP_Length(samples.count))
return result
}
private func normalizeSamples(_ samples: [Float]) -> [Float] {
var result = samples
var min: Float = 0
var max: Float = 0
vDSP_minv(samples, 1, &min, vDSP_Length(samples.count))
vDSP_maxv(samples, 1, &max, vDSP_Length(samples.count))
if max > min {
var a: Float = 1.0 / (max - min)
var b: Float = -min / (max - min)
vDSP_vsmsa(samples, 1, &a, &b, &result, 1, vDSP_Length(samples.count))
} else {
vDSP_vfill([0.5], &result, 1, vDSP_Length(samples.count))
}
return result
}
And this is how the processSamples function is used:
private func loadAudioSamples() async {
let url = recording.fileURL
if let audioFile = loadAudioFile(url: url) {
if recording.samples == nil {
recording.samples = try? await processSamples(from: audioFile)
}
}
}
private func loadAudioFile(url: URL) -> AVAudioFile? {
do {
let audioFile = try AVAudioFile(forReading: url)
return audioFile
} catch {
return nil
}
}
Any help or leads would be greatly appreciated! Thanks!
Hi all,
I have a SwiftUI-SwiftData-CloudKit app that has a TextField. I would like to make it so that after a user types in text, it will update the TextField on another device via CloudKit. I have the following code:
{
private let comment:Comment
@Query private var comments: [Comment]
@State private var text: String = ""
@State private var userInput = ""
private var filteredComment: Comment?
{
if let comment = self.comments.first(where: { $0 == self.comment })
{
return comment
}
else
{
return nil
}
}
init(comment: Comment)
{
self.comment = comment
}
var body: some View
{
VStack
{
if let filteredComment = self.filteredComment
{
TextField("", text: self.$text)
.onAppear()
{
self.text = filteredComment.text
}
.onChange(of: self.text)
{
filteredComment.text = text
}
}
}
}
}
This code actually works fine and will update to another device via CloudKit when a user exits out of the view and then reloads it. But I would like for it to update to the TextField after the user finishes typing or while they're typing. Weirdly, it will update on the fly in another screen I have in the app that displays the same text in a list. I've tried saving the modelContext after loading self.filteredComment and it makes no difference. Anyone know what's going on here? Thanks.
Hello,
i have an error with iCloud that says 'can't communicate with the server'.
Thanks!