How to delete everything that was not part of the result of importUniqueObjects? #183

Closed
opened 2025-12-29 15:26:20 +01:00 by adam · 3 comments
Owner

Originally created by @kevinrenskers on GitHub (Oct 25, 2017).

When I am importing objects from my remote server, I have something like this:

// Import objects
let objects = try transaction.importUniqueObjects(Into<T>(), sourceArray: source)

// Delete objects not part of the new collection
transaction.deleteAll(From<T>(), Where("NOT(self in %@)", objects))

So first I import the unique objects, then delete everything that wasn't part of this new collection. This used to work great! However now that I refactored to use CoreStoreObject, it's always removing all objects. Apparently this Where clause does not work properly when used with CoreStoreObject.

Is there a way to get the same functionality working with CoreStoreObject?

Originally created by @kevinrenskers on GitHub (Oct 25, 2017). When I am importing objects from my remote server, I have something like this: ``` // Import objects let objects = try transaction.importUniqueObjects(Into<T>(), sourceArray: source) // Delete objects not part of the new collection transaction.deleteAll(From<T>(), Where("NOT(self in %@)", objects)) ``` So first I import the unique objects, then delete everything that wasn't part of this new collection. This used to work great! However now that I refactored to use CoreStoreObject, it's always removing _all_ objects. Apparently this Where clause does not work properly when used with CoreStoreObject. Is there a way to get the same functionality working with CoreStoreObject?
adam closed this issue 2025-12-29 15:26:20 +01:00
Author
Owner

@kevinrenskers commented on GitHub (Oct 25, 2017):

Would I have to fetch all primary keys? I don't use a consistent property name for that though (like giftId, userId, etc) so I don't know how I would make that work?

@kevinrenskers commented on GitHub (Oct 25, 2017): Would I have to fetch all primary keys? I don't use a consistent property name for that though (like giftId, userId, etc) so I don't know how I would make that work?
Author
Owner

@kevinrenskers commented on GitHub (Oct 25, 2017):

Solved my own problem:

// Import objects
let objects = try transaction.importUniqueObjects(Into<T>(), sourceArray: source)

if !objects.isEmpty {
  // Delete objects not part of the new collection
  let ids = objects.map { $0.uniqueIDValue }
  transaction.deleteAll(From<T>(), Where("NOT(%K in %@)", type.uniqueIDKeyPath, ids))
}
@kevinrenskers commented on GitHub (Oct 25, 2017): Solved my own problem: ``` // Import objects let objects = try transaction.importUniqueObjects(Into<T>(), sourceArray: source) if !objects.isEmpty { // Delete objects not part of the new collection let ids = objects.map { $0.uniqueIDValue } transaction.deleteAll(From<T>(), Where("NOT(%K in %@)", type.uniqueIDKeyPath, ids)) } ```
Author
Owner

@JohnEstropia commented on GitHub (Oct 26, 2017):

@kevinrenskers You may also want to use the ! operator on Where:

transaction.deleteAll(From<T>(), !Where("SELF in %@", ids))

I recommend that you go through the README first. It's long but you'll find a lot of convenient way for common tasks.

@JohnEstropia commented on GitHub (Oct 26, 2017): @kevinrenskers You may also want to use the `!` operator on `Where`: ```swift transaction.deleteAll(From<T>(), !Where("SELF in %@", ids)) ``` I recommend that you go through the README first. It's long but you'll find a lot of convenient way for common tasks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#183