UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to populate the Bookworm project with books

Forums > macOS

In the hacking with macOS book for the Bookworm project I would like to initially populate with some books. I tried a couple of things but don't see how to populate the arrary controller with data initially. Can this be done, if so how?

3      

hi,

one possibility is that you simply create new books in code at startup, once the Core Data stack is established, in the same way that you do AddBook() elsewhere in the code. it's been awhile since i did a MacOS app, but i'd look at adding the code in the main NSViewController's viewDidLoad() method.

a second, much cleaner suggestion is to preload the Core Data store using JSON data from the app bundle. i don't know if this is covered directly in Hacking with MacOS; but you will find it in the 100 Days courses (both UI-Kit based Swift and SwiftUI versions). search for the Moonshot and SnowSeeker projects, and anything having to do with Codable and Core Data objects. although these are primarily iOS projects, the techniques involving Core Data and JSON are the same.

of course, once data is pre-populated, you'd not want to load the same data again, so first check that the Core Data store is empty before loading pre-defined book data.

hope that helps,

DMG

4      

Thanks DMG for the reply.

I beginning to undestand using an Array Controler as Paul presents in his book. Also, I see why Paul is not hot on cocoa bindings as in the bookworm project.. Two major issuess ArrayControlles/Cocoa Bindings difficult to debug as well as code can be hacked. However, this approach does provide a lot of function under the covers. Like sorting columns, editing, deleting and filtering (I have another question on this). So, I am writing an App that gets its data from an SQLite table actually several tables so a nice clean consitant interface is attractive.

With this said I tried to populate the reviews with 5 books thinking this might be all that I would need, then I could translate that to the SQLite data but no not that simple.

Also, I don't see the AddBook function you mentioned in your reply.

As you can tell I am new at this (lots of expierence with other lang/environments) so, any advice or solution is greatly appreciated.

Bob

3      

hi,

i think we might be ships passing in the night on this, as i understood your Bookworm reference in the context of an app that uses Core Data (Paul's Bookworm for iOS is the first example of using Core Data in 100 Days of SwiftUI, day 53, project 11). i have not done anything with SQLite tables directly (only through the Core Data layer on top of it).

sorry about that, and the specific AddBook() reference.

surely, at some point in your code, you currently allow the user to enter and save a new book (a title, an author, a review, a rating, etc.). i only proposed that to get the app started, you could do either of these:

(1) take the cheap way for now: if your persistent store is empty at startup, throw a few books into the store with the same code as you would add a user's entry of a new book.

(2) look into loading (JSON) data for book content straight from your app's bundle on startup. Paul has plenty of info on this on the site, and almost all of it is generic Swift that works on both iOS and MacOS. you would need to know about Codable to accomplish this.

again, sorry if i have missed the boat (!) on this.

good luck,

DMG

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.