Core Data: Beyond the Basics

Jul 26 2022 · Swift 5.5, iOS 15, Xcode 13.3.1

Part 1: Fetching & Displaying Launches

09. Challenge - Adding Tags

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 08. Compound Predicates Next episode: 10. Challenge - Displaying Tags

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You’ve learned quite a bit and you haven’t really taken a break in between to let it all sink in. So before you do anything else, I want you to practice what you have learned so far with a fairly sizable challenge.

@NSManaged public var launches: Set<RocketLaunch>
static func fetchOrCreateWith(title: String, in context: NSManagedObjectContext) -> Tag {}
let request: NSFetchRequest<Tag> = fetchRequest()
let predicate = NSPredicate(format: "%K == %@", "title", title.lowercased())
request.predicate = predicate
do {

} catch {
  fatalError("Error fetching tags")
}
let results = try context.fetch(request)
if let tag = results.first {
  return tag
}
else {

}
let tag = Tag(context: context)
tag.title = title.lowercased()
return tag
@NSManaged var tags: Set<Tag>?
//static func createWith(
//	name: String,
//	notes: String,
//	launchDate: Date,
//	isViewed: Bool,
//	launchpad: String,
	tags: Set<Tag> = [],
//	in list: RocketLaunchList,
//	using managedObjectContext: NSManagedObjectContext
//	) {
//	let launch = RocketLaunch(context: managedObjectContext)
//	launch.name = name
//	launch.notes = notes
//	launch.launchDate = launchDate
//	launch.isViewed = isViewed
//	launch.launchpad = launchpad
	launch.tags = tags
//	launch.addToList(list)
	
//	do {
//	  try managedObjectContext.save()
//	} catch {
//	  let nserror = error as NSError
//	  fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
//	}
//}
Section {
  TextField("Tags", text: $tags)
}
@State var tags: String = ""
let tags = Set(self.tags.split(separator: ",").map { Tag.fetchOrCreateWith(title: String($0), in: self.viewContext) })
//RocketLaunch.createWith(
//	name: self.text,
//	notes: self.notes,
//	launchDate: self.launchDate,
//	isViewed: false,
//	launchpad: self.launchPad,
	tags: tags,
//	in: self.launchList,
//	using: self.viewContext)
List: Test Flights
Launch title: Test flight 1
Tags: SpaceX, texas, test