1

I created a Core Data object as follows:

@objc(Gates)
public class Gates : NSManagedObject {

    public class func getFetchRequest() -> NSFetchRequest<Gates> {
        let request = NSFetchRequest<Gates>(entityName: "Gates")
        request.returnsObjectsAsFaults = false
        return request
    }

    @NSManaged var updatedAt: String
    @NSManaged var objectId: String
    @NSManaged var identifier: String
    @NSManaged var name: String
    @NSManaged var address: String
    @NSManaged var dueDate: String
    @NSManaged var productionCode: String
    @NSManaged var locationCountry: String
    @NSManaged var locationCity: String
    @NSManaged var locationBuilding: String
    @NSManaged var locationLevel: String
    @NSManaged var locationRoom: String
    @NSManaged var locationRange: String
    @NSManaged var isFavorite: Bool

    public func setGateData(gateDict: [String: Any]) {
        updatedAt = gateDict["updatedAt"] as? String ?? ""
        objectId = gateDict["objectId"] as? String ?? ""
        identifier = gateDict["identifier"] as? String ?? ""
        name = gateDict["name"] as? String ?? ""
        isFavorite = gateDict["isFavorite"] as? Bool ?? false
        address = gateDict["address"] as? String ?? ""
        dueDate = gateDict["dueDate"] as? String ?? ""
        productionCode = gateDict["productionCode"] as? String ?? ""
        locationCountry = gateDict["locationCountry"] as? String ?? ""
        locationCity = gateDict["locationCity"] as? String ?? ""
        locationBuilding = gateDict["locationBuilding"] as? String ?? ""
        locationLevel = gateDict["locationLevel"] as? String ?? ""
        locationRoom = gateDict["locationRoom"] as? String ?? ""
        locationRange = gateDict["locationRange"] as? String ?? ""
    }
}

I also set this up in the xcdatamodeld:

enter image description here

Now, after I have saved the object in core data and I'm using the getFetchRequest() method that is part of the class which sets request.returnsObjectsAsFaults = false on the request but I still getting the following result when I try to print the fetched objects:

 <Gates: 0x60c0000959a0> (entity: Gates; id: 0xd000000005e40000 <x-
 coredata://B9C33A5D-BF96-433A-9186-F51AA253F488/Gates/p377> ; data: <fault>)

As you can see in this case the data is still data: <fault>.

Why is the object parameters are not retrieved even though I set request.returnsObjectsAsFaults = false? What am I missing?

halfer
  • 19,824
  • 17
  • 99
  • 186
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • 2
    One common reason is, if your fetch request is executed from a child context who answers to a parent context rather than directly to the persistent coordinator. Try `moc.persistentCoordinator.fetch...` instead of `moc.fetch...` – Lukas Mar 21 '18 at 20:31
  • Hi Emil. As a 25K user, I expect you're aware that a technical standard of writing is desired here. If you can refrain from adding advance thanks to your questions, that will reduce the workload of volunteer editors. Also, titles should stand alone - they should not have a follow-on colon at the end. Thanks if you can refrain from these things in your future posts! – halfer Mar 23 '18 at 12:19
  • 4
    @halfer, To tell you the truth, for a couple of weeks I see that you edit specifically my questions or answers. And Yes I like to thank people in advance for their help and also after they help me, and yes I like to focus people on what really my question is by bolding the location of my question because maybe they don't even have to read all the explanation and just know the answer to the marked question. Before you started editing my questions I didn't see anyone else bothered by it. So if this bothers you I would suggest just to refrain from systematically visiting my questions and answers. – Emil Adz Mar 23 '18 at 14:30
  • "I didn't see anyone else bothered by it" - well, fair enough. I hope that once you see the following links, you will understand that it's a community opinion, not just me. Here is [the main one](https://meta.stackoverflow.com/questions/288160/no-thanks-damn-it) and [this is relevant as well](https://meta.stackoverflow.com/questions/260776/should-i-remove-fluff-when-editing-questions). Thanks for your thoughtful consideration, and being open to changing your mind on the matter. – halfer Mar 23 '18 at 14:46
  • (I'm not focussing on _just_ you - I have a list of around 150 folks who I think warrant specific editing attention). – halfer Mar 23 '18 at 14:48
  • 3
    @halfer, Ok I went over the links you provided. One thing I also noticed there is that there is no reason to edit the post just for the sake of removing the "thanks in advance" which is what most of your edits to my posts are. Now, let me ask you a question. How do you know that removing the "Question is" didn't contributed to the fact that this question still doesn't have an answer? Maybe if the people saw this and knew what the focus of the question is they will be more willing to provide an answer to the question in focus? – Emil Adz Mar 23 '18 at 18:20
  • I think you might have misread a Meta post. Yes, we ask editors not to focus on just one thing if there are other major issues to correct. However, if someone's posts are otherwise OK except for one thing, it's _is_ fine to adjust that. As for the announcement that a question is a question, it strikes me as both obvious and redundant, given the community's preference for brevity, but it's not as chatty as thanks, so I personally wouldn't remove it if that was the only issue. – halfer Mar 24 '18 at 15:20
  • 3
    Then I will be very grateful if you leave the "question is" in my questions. In my opinion, it focuses people on the question at hand. One reason for that is that for a lot of people here the English language is not their first language exactly as it's not for me, and this just lets them see what is the most important part of the question is. I don't mind you removing the "thanks". – Emil Adz Mar 26 '18 at 08:53
  • Alright, I will give way on the summary text, with the proviso that "question" and "problem" are not [proper nouns](https://en.wikipedia.org/wiki/Proper_noun), and are thus not capitalised. – halfer Apr 01 '18 at 12:22
  • @EmilAdz Have you found any solution or reason why it's happening I am also facing same issue. – The iCoder Feb 05 '20 at 11:51

1 Answers1

0

I'm having this issue and I found in my case instead of using the objects value in line, I initialize a variable with it first and then use that variable.

I would love to know if this is a Core Data bug or if I'm doing something wrong.

public class Person: NSManagedObject, Identifiable {
    @NSManaged public var firstName: String
    @NSManaged public var lastName: String
    @NSManaged public var emailAddress: String
}

This does not work all the time:


CoreDataManager.shared.persistentContainer.performBackgroundTask{ context in
    context.automaticallyMergesChangesFromParent = true
    context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    
    do {
        let email = "123Tester@gmail.com"
        let request = Person.getPersonWith(email: email)
        request.returnsObjectsAsFaults = false //DOES NOT WORK
        
        if let person = try context.fetch(request).first{
            print(person.fullName)
        }
    } catch{
        fatalError()
    }
}

However this does

CoreDataManager.shared.persistentContainer.performBackgroundTask{ context in
    context.automaticallyMergesChangesFromParent = true
    context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    
    do {
        let email = "123Tester@gmail.com"
        let request = Person.getPersonWith(email: email)
        
        if let person = try context.fetch(request).first{
            let fullName = person.fullName
            print(fullName)
        }
    } catch{
        fatalError()
    }
}
Richard Witherspoon
  • 4,082
  • 3
  • 17
  • 33