Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data <Fault > after use one time

I search for hours but didn't find any thing

i am in wired situation in Core Data

method to fetch object

func fetchAllCoreData() -> NSArray? {


    let moc = DataController().managedObjectContext
    let fetchReq = NSFetchRequest(entityName: "Theater")
    var array =  [Theater]()
    fetchReq.returnsObjectsAsFaults = false
    do {
        let obj = try moc.executeFetchRequest(fetchReq) as!  [Theater]
        for var details in obj {


            array.append(details)
        }

    } catch {
        print("Error in Fetching")
        return nil
    }

    return array
}

where DataController() is from http://www.codebeaulieu.com/10/adding-core-data-using-swift-2

i got values from fetch is

 po print(self.arrTheater)

[ (entity: Theater; id: 0x7bf67a00 ; data: { address = Iskon; city = Ahmedabad; id = 1; isFav = 1; name = PVR; }), (entity: Theater; id: 0x7bf5ee60 ; data: { address = "Prahladnagar "; city = Ahmedabad; id = 2; isFav = 0; name = Cinemex; }), (entity: Theater; id: 0x7bf5ee70 ; data: { address = "New Address"; city = Rajkot; id = 3; isFav = 0; name = "Blue Game"; }), (entity: Theater; id: 0x7bf5ee80 ; data: { address = "New ISKON"; city = Porbandar; id = 4; isFav = 1; name = JaiHind; }), (entity: Theater; id: 0x7bf55120 ; data: { address = "New Address "; city = Mumbai; id = 5; isFav = 0; name = "Drama "; }), (entity: Theater; id: 0x7bf55130 ; data: { address = "Vijay 4 Road"; city = Ahmedabad; id = 6; isFav = 1; name = PVR; }), (entity: Theater; id: 0x7bf55140 ; data: { address = "Mahavir Nagar"; city = Delhi; id = 7; isFav = 0; name = Cinemex; }), (entity: Theater; id: 0x7bf28570 ; data: { address = Iskon; city = Ahmedabad; id = 8; isFav = 0; name = "Wide Angle"; })]

this method is called from viewDidLoad After that i reload tableview

There is nothing in Tableview cellForRow

one line to set value to title label and all display good

then after i just scroll tableview or did select tableview

and then i print

FAULT as value of data

i read theory and found that it is just place holder but i am not able to fetch that

please help me why it shows data first time and then shows fault

i already fetchReq.returnsObjectsAsFaults = false

Thanks in Advance

like image 202
Prashant Tukadiya Avatar asked May 03 '16 14:05

Prashant Tukadiya


2 Answers

Faults are in general not something to worry about (CoreData will automatically fire the fault and populate the object as soon as you access any of its attributes).

But in this instance I suspect something more nefarious is happening: the DataController is instantiated locally in that fetchAllCoreData method, and I fear it is being deallocated (together with the associated managedObjectContext) as soon as the method completes. Your return array consequently contains NSManagedObjects from a context which has been deallocated. To check, try making moc a property rather than a local variable. That should be enough to keep a permanent reference to the context (which in turn will have strong references to the underlying stack) and prevent it being deallocated.

Longer term you should consider having a property for the DataController itself, and either make it a singleton so you can access it from any view controller, or pass it from view controller to view controller.

like image 71
pbasdf Avatar answered Nov 14 '22 16:11

pbasdf


I had the same problem. I was fetching the data from database (and saving it to a local array) and then populating a table. Later after few seconds if I click on any content , the app was crashing. All the NSManagedObjects were faulted by that time.

The issue was, I was calling an api in between and on the success response, I was deleting all the objects in the database and adding new content from api response. Since NSManagedObjects are rendered dynamically , the old data in the local array which I used to populate the table was faulted.

If anybody is facing this kind of issue, first try setting this:

urRequest.returnsObjectsAsFaults = false 

If this doesn't help, just check if you are modifying the database in between (ie; after fetching for the first time).

like image 43
abhimuralidharan Avatar answered Nov 14 '22 14:11

abhimuralidharan