the-blue-alliance / the-blue-alliance-ios

An iOS app for accessing information about the FIRST Robotics Competition.
MIT License
76 stars 23 forks source link

Zorr/rewrite tbakit tbadata #940

Open ZachOrr opened 2 years ago

ZachOrr commented 2 years ago

Notes

throw in findOrCreate

API models might have other dependent API models. Ex: An Award needs an Event. The Award's insert method might look like the following -

static func insert(_ model: APIAward, in context: NSManagedObjectContext) async throws -> TBAAward {
  let predicate = ...
  return try await findOrCreate(in: context, matching: predicate) { (award) in
    award.event = try TBAEvent.insert(model.eventKey, in: context)
  }
}

We do a findOrCreate via the Event.insert method as opposed to passing the TBAEvent during the TBAAward.insert because the context of inserting an Award does not guarantee having an Event object.

This insert is problematic though because the insert can throw. Even in a minimal situation where we insert an Event object with just a key, our Core Data fetch (the find half of findOrCreate) can fail.

Misc