0

I have usual SwiftUI view like this

struct MyView: View {


    @FetchRequest var users: FetchedResults<User>

    init() { 
       self._users = FetchRequest(
        entity: User.entity(),
        sortDescriptors: [

        ],
        predicate: NSPredicate(format: "company.id == %@", companyId)
    )
    }

    var body: some View {

        List {
            ForEach(Array(self.users.enumerated()), id: \.1.objectID) { (i, user) in
                Text("\(user.name)")
            }
        }
}

But after locking screen/using home button and returning to app. Initially this view wake ups with empty NSManagedObjects, objects seems to be available there is correct users.count value, each object has its appropriate objectID. But other managed object properties are nil. Then sometimes I experience that in subsequent view refreshes it can (I think "fault" this object properties) fetch this properties and displays ok, or can stay with nil values and I have empty results on list or crash depending user.name is forced unwrap or not

user.name! or user.name ?? ""
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
  • i do not think that this piece of code could help us with your problem. maybe you should show us copyable runnable code so we can reproduce your problem.... – Chris Apr 08 '20 at 08:20
  • To have fully runnable code I should paste thousands of lines like Core Data models, View nesting. But here I rather ask for conception why sleeping phone and waking it app can cause that @FetchRequest list of objects can contain objects that has all fields empty in spite of object id. I think it is easily replicable by making simple view and model like in my example. I can paste 10 snippets with thousands of lines of code but it will confuse even more. So I tried to limit problem scope to just this nil properties on users objects. It happens in each cases I have several such views – Michał Ziobro Apr 08 '20 at 09:20
  • i did not say the complete project ;) but you could break down to just that lines of codes where it happens and it should be compilable/runnable, that's all what i say. – Chris Apr 08 '20 at 10:47
  • Above code is compilable if you add core data model with User. I can add init() where I create FetchRequest – Michał Ziobro Apr 08 '20 at 10:48
  • yes, compilable, but not runnable if you don't add company + id so you need more than just user ;) – Chris Apr 08 '20 at 11:29
  • No you can change predicate and just take all users from User entity it doesn't matter in the case of this issue i.e. losing properties of managed objects fetched with @FetchRequest – Michał Ziobro Apr 08 '20 at 11:53

0 Answers0