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)->VoidinletuniqueIDValue="a unique id string"varmadeEvent=transaction.fetchOne(From(Event),Where("uid == %@",uniqueIDValue))ifmadeEvent==nil{madeEvent=transaction.create(Into(Event))}guardleteventObj=madeEventelse{return}eventObj.uid=uniqueIDValue//Set any other attributestransaction.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 18:21:21 +01:00
@PaulWoodIII
Personally I'll just write it like this
CoreStore.beginAsynchronous{(transaction)->VoidinletuniqueIDValue="a unique id string"letevent=transaction.fetchOne(From(Event),Where("uid == %@",uniqueIDValue))??transaction.create(Into(Event))event.uid=uniqueIDValue//Set any other attributestransaction.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.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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
@JohnEstropia commented on GitHub (Jul 31, 2015):
@PaulWoodIII
Personally I'll just write it like this
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,
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.