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

Day 61 - Time for Core Data

Forums > 100 Days of SwiftUI

Hello,

I'm trying to build an app that fetches data online via URLSession the first time it is launched, but in successive launches attempts to use coreData. I am not sure how to prevent the app from trying to load data from online.

Originally I was running .onAppear(perform: loadData()) where loadData() get the JSON information online. However this will run whenever the view is launched so I need to first check if the fetch request is empty.

Below is my attempt to create a custom initializer, but this does not compile. I am not quite sure why as the error messages are a little bit unclear; but to be honest this doesn't look completely right to begin with. Any ideas?

struct ContentView: View {
  @Environment(\.managedObjectContext) private var viewContext

  @FetchRequest(
    sortDescriptors: [],
    animation: .default) private var fetched_users: FetchedResults<Item>

  var users: [User]

  var body: some View {
    Text("Hi")
  }

  init(){
    if fetched_users.count() > 0{
      self.users = fetched_users
    }

    else{
      loadData()
    }
  }

2      

I can see why it wouldn't compile. You are trying to use fetched_users in your initializer for creating an instance of ContentView, but that variable wouldn't even exist until an instance of ContentView has already been created.

I don't know the proper way to do what you are trying to do though.

2      

hi,

i see a few problems:

  • it looks like you are trying to set up a @FetchRequest for objects whose class is an Item, and i would think you'd want to write something like
    @FetchRequest(entity: Item.entity(),
    sortDescriptors: [],
    animation: .default) private var fetched_users: FetchedResults<Item>
  • if you are fetching Item objects from Core Data, you cannot assign them to the users array of type [User]. (unless, of course, there's a class/subclass relationship.)
  • you cannot access the values of a @FetchRequest in a custom init() method. the @FetchRequest is only useable after the View is initialized.

you might go back to loading the users array in .onAppear using the code you are trying to write for the init(), and skip the custom initializer altogether.

hope that helps,

DMG

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.