核心数据<故障>使用一次后 [英] Core Data <Fault > after use one time
问题描述
我搜索了几个小时,但没有找到任何东西
我处于核心数据 $ b中
$ b
要提取对象的方法
func fetchAllCoreData()-> ; NSArray? {
let moc = DataController()。managedObjectContext
let fetchReq = NSFetchRequest(entityName: Theater)
var array = [Theater]()
fetchReq.returnsObjectsAsFaults =假
做{
让obj =试试moc.executeFetchRequest(fetchReq)为! [剧院]
用于obj {
array.append(details)
}
} catch {
print(提取错误)
return nil
}
return array
}
其中 DataController()来自 http://www.codebeaulieu.com/10/adding-core-data-using-swift-2
我从获取中得到的值是
po print(self.arrTheater)
[(实体:剧院; ID:0x7bf67a00;数据:{
地址= Iskon;
城市=艾哈迈达巴德(Ahmedabad);
id = 1;
isFav = 1;
名称= PVR;
}),(实体:剧院; ID:0x7bf5ee60;数据:{
地址= Prahladnagar;
城市=艾哈迈达巴德;
id = 2;
isFav = 0;
名称= Cinemex;
}),(实体:剧院; id :0x7b f5ee70;数据:{
地址=新地址;
城市=拉杰果德;
id = 3;
isFav = 0;
name = Blue Game;
}),(实体:剧院; id:0x7bf5ee80;数据:{
地址=新ISKON;
城市= Porbandar;
id = 4;
isFav = 1;
名称= JaiHind;
}),(实体:剧院; id:0x7bf55120;数据:{
地址=新地址;
城市=孟买;
id = 5;
isFav = 0;
name = Drama;
}),(实体:剧院; ID:0x7bf55130;数据:{
地址= Vijay 4 Road;
城市=艾哈迈达巴德;
id = 6;
isFav = 1;
名称= PVR;
}),(实体:剧院; id:0x7bf55140; data:{
地址= Mahavir Nagar;
城市=德里;
id = 7;
isFav = 0;
名称= Cinemex ;
}),(实体:剧院; id:0x7bf28570;数据:{
地址= Iskon;
城市=艾哈迈达巴德;
id = 8;
isFav = 0;
name = Wide Angle;
})]
此方法从viewDidLoad
之后调用我重新加载tableview
Tableview cellForRow中没有任何内容
一行用于设置标题标签的值
一切都显示良好
然后我只是滚动tableview或选择了tableview
然后我打印
故障作为数据值
我读了理论,发现它只是地方持有人,但我无法提取
请帮我解释为什么它第一次显示数据然后显示 fault
我已经 fetchReq.returnsObjectsAsFaults = false
先行感谢
一般而言,无需担心故障(CoreData会在您访问其任何对象后自动触发该故障并填充该对象属性)。
但是在这种情况下,我怀疑正在发生更邪恶的事情: DataController
在该本地实例化为 fetchAllCoreData
方法,我担心该方法完成后会立即被释放(连同相关的 managedObjectContext
)。因此,您的返回数组包含来自已释放上下文的NSManagedObjects。要进行检查,请尝试将 moc
设置为属性而不是局部变量。这样就足以保持对上下文的永久引用(反过来又对基础堆栈具有强大的引用)并防止将其释放。
长期而言,您应该考虑为 DataController
本身提供一个属性,并使其成为单例,以便您可以从任何视图控制器访问它,或者将其从视图控制器传递到视图控制器。
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
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.
这篇关于核心数据<故障>使用一次后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!