@@ -340,34 +340,50 @@ extension tableMetadata {
340340 CGSize ( width: width, height: height)
341341 }
342342
343+ var tagNames : [ String ] {
344+ tags. map ( \. name)
345+ }
346+
343347 /// Returns false if the user is lokced out of the file. I.e. The file is locked but by somone else
344348 func canUnlock( as user: String ) -> Bool {
345349 return !lock || ( lockOwner == user && lockOwnerType == 0 )
346350 }
347351
348352 /// Returns a detached (unmanaged) deep copy of the current `tableMetadata` object.
349353 ///
350- /// - Note: Primitive list properties (e.g., `shareType`) are copied automatically by `init(value:)`.
351- /// For `List` containing Realm objects (e.g., `exifPhotos`, `tags`) this method creates new instances
352- /// to ensure the copy is fully detached and safe to use outside of a Realm context.
354+ /// - Note: Primitive properties and lists of primitive values (for example `shareType`)
355+ /// are copied automatically by `init(value:)`.
356+ /// For `List` properties containing Realm objects (for example `exifPhotos` and `tags`),
357+ /// this method recreates each element explicitly to ensure the resulting copy is fully
358+ /// detached and safe to use across Realm contexts.
353359 ///
354360 /// - Returns: A new `tableMetadata` instance fully detached from Realm.
355361 func detachedCopy( ) -> tableMetadata {
356- // Use Realm's built-in copy constructor for primitive properties and List of primitives
362+ // Use Realm's built-in copy constructor for primitive properties and lists of primitive values.
357363 let detached = tableMetadata ( value: self )
358364
359- // Deep copy of List of Realm objects (exifPhotos and tags)
365+ // Deep copy of List of Realm objects
360366 detached. exifPhotos. removeAll ( )
361- detached. exifPhotos. append ( objectsIn: self . exifPhotos. map { NCKeyValue ( value: $0) } )
367+ detached. exifPhotos. append ( objectsIn: self . exifPhotos. map {
368+ let copy = NCKeyValue ( )
369+ copy. key = $0. key
370+ copy. value = $0. value
371+ return copy
372+ } )
373+
362374 detached. tags. removeAll ( )
363- detached. tags. append ( objectsIn: self . tags. map { tableMetadataTag ( value: $0) } )
375+ detached. tags. append ( objectsIn: self . tags. map {
376+ let copy = tableMetadataTag ( )
377+ copy. primaryKey = $0. primaryKey
378+ copy. account = $0. account
379+ copy. id = $0. id
380+ copy. name = $0. name
381+ copy. color = $0. color
382+ return copy
383+ } )
364384
365385 return detached
366386 }
367-
368- var tagNames : [ String ] {
369- tags. map ( \. name)
370- }
371387}
372388
373389extension NCManageDatabase {
@@ -474,16 +490,6 @@ extension NCManageDatabase {
474490 }
475491 }
476492
477- func addMetadataIfNotExistsAsync( _ metadata: tableMetadata ) async {
478- let detached = metadata. detachedCopy ( )
479-
480- await core. performRealmWriteAsync { realm in
481- if realm. object ( ofType: tableMetadata. self, forPrimaryKey: metadata. ocId) == nil {
482- realm. add ( detached)
483- }
484- }
485- }
486-
487493 func deleteMetadataAsync( predicate: NSPredicate ) async {
488494 await core. performRealmWriteAsync { realm in
489495 let result = realm. objects ( tableMetadata. self)
0 commit comments