Uniquing an Object #8

Closed
opened 2025-12-29 15:21:58 +01:00 by adam · 1 comment
Owner

Originally created by @PaulWoodIII on GitHub (Jul 31, 2015).

I've got a recommendation for some additional documentation, and by the way your documentation is great, Its obvious that you care so thats why I'm recommending some more

How to fetch or create a unique object, below is my current way of doing it but I'd like to know your way and I think others would like to know as well

CoreStore.beginAsynchronous { (transaction) -> Void in
    let uniqueIDValue = "a unique id string"
    var madeEvent = transaction.fetchOne(From(Event), Where("uid == %@",uniqueIDValue))
    if madeEvent == nil {
        madeEvent = transaction.create(Into(Event))
    }
    guard let eventObj = madeEvent else {
        return
    }
    eventObj.uid = uniqueIDValue
    //Set any other attributes
    transaction.commit()
}
Originally created by @PaulWoodIII on GitHub (Jul 31, 2015). I've got a recommendation for some additional documentation, and by the way your documentation is great, Its obvious that you care so thats why I'm recommending some more How to fetch or create a unique object, below is my current way of doing it but I'd like to know your way and I think others would like to know as well ``` swift CoreStore.beginAsynchronous { (transaction) -> Void in let uniqueIDValue = "a unique id string" var madeEvent = transaction.fetchOne(From(Event), Where("uid == %@",uniqueIDValue)) if madeEvent == nil { madeEvent = transaction.create(Into(Event)) } guard let eventObj = madeEvent else { return } eventObj.uid = uniqueIDValue //Set any other attributes transaction.commit() } ```
adam added the discussion label 2025-12-29 15:21:58 +01:00
adam closed this issue 2025-12-29 15:21:59 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jul 31, 2015):

@PaulWoodIII
Personally I'll just write it like this

CoreStore.beginAsynchronous { (transaction) -> Void in
    let uniqueIDValue = "a unique id string"
    let event = transaction.fetchOne(From(Event), Where("uid == %@",uniqueIDValue))
        ?? transaction.create(Into(Event))
    event.uid = uniqueIDValue
    //Set any other attributes
    transaction.commit()
}

But that's totally just aesthetic preference and depends entirely on other requirements. You should be free to decide your own best practice.

Your comment's timing is really good though. The next in CoreStore's roadmap is Data importing utilities for transactions. While the fetch-or-create pattern is common, part of the optimization utilities I'm designing is for uniquing multiple objects when importing from an external source. In pseudo-code,

  • get all IDs from the external source
  • fetch all existing objects in one fetch request using the array of IDs
  • update each fetched object
  • create the rest of the non-existent IDs
  • update each created object

Instead of doing 1 fetch/create each loop, this saves a lot of I/O by doing all fetches in one request and just loop creation on the non-existent items.

@JohnEstropia commented on GitHub (Jul 31, 2015): @PaulWoodIII Personally I'll just write it like this ``` swift CoreStore.beginAsynchronous { (transaction) -> Void in let uniqueIDValue = "a unique id string" let event = transaction.fetchOne(From(Event), Where("uid == %@",uniqueIDValue)) ?? transaction.create(Into(Event)) event.uid = uniqueIDValue //Set any other attributes transaction.commit() } ``` But that's totally just aesthetic preference and depends entirely on other requirements. You should be free to decide your own best practice. Your comment's timing is really good though. The next in CoreStore's roadmap is _Data importing utilities for transactions_. While the fetch-or-create pattern is common, part of the optimization utilities I'm designing is for uniquing multiple objects when importing from an external source. In pseudo-code, - get all IDs from the external source - fetch all existing objects in one fetch request using the array of IDs - update each fetched object - create the rest of the non-existent IDs - update each created object Instead of doing 1 fetch/create each loop, this saves a lot of I/O by doing all fetches in one request and just loop creation on the non-existent items.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#8