我的所有数据都没有存储在 Coredata
Posted
技术标签:
【中文标题】我的所有数据都没有存储在 Coredata【英文标题】:My all data are not getting stored in Coredata 【发布时间】:2016-11-04 09:56:06 【问题描述】:我正在从服务器获取值并将它们存储到 Coredata。问题来了,当我从服务器获取数据并将它们存储在 coredata 中时,它表明我已经存储了所有值.....
这些是我存储的数据:
[
adName = Britannia;
adType = Video;
addescription = "Choco Muffills come with thick chocolate filling that will take you by surprise.";
clientImage = "http://api.weoneapp.com:8595/1464528609370.png";
clientName = Britannia;
endDate = "Just now";
entryId = 1478230186228;
likeFlag = 0;
mainImage = "";
newAdvertisement = 1;
noOfCount = 1367;
noOfLike = 2783;
seenAdvertisement = 0;
smilURL = "http://52.66.101.222:1935/vod/_definst_/1478230186228/smil:1478230186228.smil/playlist.m3u8";
sortDate = "2016-11-03T19:27:00.000Z";
startDate = "2016-11-03T19:27:00.000Z";
subTitle = "Britannia Cake Muffills";
thumbNail = "http://52.66.101.222:8595/video/1478230186228/1478230186228_thumbnail.png";
videoDownloadUrl = "http://52.66.101.222:8595/video/1478230186228/1478230186228_source.mp4";
,
adName = "Kwality Walls";
adType = Video;
addescription = "Paddle Pop Yummy Delicious Dessert - A delicious, colourful and yummy dessert ... Walls India \U00b7 Kulfeez from Kwality Walls India \U00b7";
clientImage = "http://api.weoneapp.com:8595/1464524885184.jpg";
clientName = "Kwality Walls";
endDate = "Just now";
entryId = 1478230623594;
likeFlag = 0;
mainImage = "";
newAdvertisement = 1;
noOfCount = 1281;
noOfLike = 2618;
seenAdvertisement = 0;
smilURL = "http://52.66.101.222:1935/vod/_definst_/1478230623594/smil:1478230623594.smil/playlist.m3u8";
sortDate = "2016-11-03T18:36:00.000Z";
startDate = "2016-11-03T18:36:00.000Z";
subTitle = "Paddle Pop Jiggly Jelly";
thumbNail = "http://52.66.101.222:8595/video/1478230623594/1478230623594_thumbnail.png";
videoDownloadUrl = "http://52.66.101.222:8595/video/1478230623594/1478230623594_source.mp4";
,
adName = "Cadbury Dairy Milk";
adType = Video;
addescription = "Your favourite chocolate is now more chocolatey.";
clientImage = "http://api.weoneapp.com:8595/1464017173884.jpg";
clientName = Cadbury;
endDate = "Just now";
entryId = 1478230473291;
likeFlag = 0;
mainImage = "";
newAdvertisement = 0;
noOfCount = 1310;
noOfLike = 2600;
seenAdvertisement = 1;
smilURL = "http://52.66.101.222:1935/vod/_definst_/1478230473291/smil:1478230473291.smil/playlist.m3u8";
sortDate = "2016-11-03T18:32:00.000Z";
startDate = "2016-11-03T18:32:00.000Z";
subTitle = "Cadbury Dairy Milk";
thumbNail = "http://52.66.101.222:8595/video/1478230473291/1478230473291_thumbnail.png";
videoDownloadUrl = "http://52.66.101.222:8595/video/1478230473291/1478230473291_source.mp4";
]
现在,当我转到位于以下位置的 .sqlite 文件夹时:/Users/Johnn/Library/Developer/CoreSimulator/Devices/0DF66BFE-6076-45E9-8325-D61F1C738DFA/data/Containers/Data /Application/DC3F7B17-1172-4AE3-BC3B-217624C21AA5/Documents/AppCoreData.sqlite
并打开这个 .sqlite 文件> 转到我找到的表:
只存储了两个值,我的最后一个值是空的。
我不知道这背后的原因是什么,为什么我的数组的最后一个值没有存储在数据库中......
我正在存储这样的值:
class func createInManagedObjectContext(nameOfAdvert: String, thumbnail: String, fileName: String, videoDownloadUrl: String, subTitle: String, adType: String, mainImage: String, noOfLike: NSNumber, entryId: String, likeFlag: Bool, startDate: String, endDate: String, noOfCount: NSNumber ,clientName: String, clientImage: String, adDesc: String, sortDate: String, newAdvertisement: Bool, seenAdvertisement: Bool) -> Tube
let managedObjectContext = DataAccess.shared.managedObjectContext
let newItems = NSEntityDescription.insertNewObjectForEntityForName("Tube", inManagedObjectContext: managedObjectContext) as! Tube
do
try managedObjectContext.save()
newItems.adName = nameOfAdvert
newItems.thumbNail = thumbnail
newItems.smilURL = fileName
newItems.videoDownloadUrl = videoDownloadUrl
newItems.subTitle = subTitle
newItems.adType = adType
newItems.mainImage = mainImage
newItems.noOfLike = noOfLike
newItems.entryId = entryId
newItems.likeFlag = likeFlag
newItems.startDate = startDate
newItems.endDate = endDate
newItems.noOfCount = noOfCount
newItems.clientImage = clientImage
newItems.clientName = clientName
newItems.addescription = adDesc
newItems.sortDate = sortDate
newItems.newAdvertisement = newAdvertisement
newItems.seenAdvertisement = seenAdvertisement
catch let error as NSError
print("Could not save \(error), \(error.userInfo)")
print("-----------------------------")
print(newItems)
return newItems
我不知道为什么所有数据都没有保存在 coredata 中。任何帮助将不胜感激。
提前致谢
【问题讨论】:
【参考方案1】:请写下这段代码:->
插入数据后写try managedObjectContext.save()
。
class func createInManagedObjectContext(nameOfAdvert: String, thumbnail: String, fileName: String, videoDownloadUrl: String, subTitle: String, adType: String, mainImage: String, noOfLike: NSNumber, entryId: String, likeFlag: Bool, startDate: String, endDate: String, noOfCount: NSNumber ,clientName: String, clientImage: String, adDesc: String, sortDate: String, newAdvertisement: Bool, seenAdvertisement: Bool) -> Tube
let managedObjectContext = DataAccess.shared.managedObjectContext
let newItems = NSEntityDescription.insertNewObjectForEntityForName("Tube", inManagedObjectContext: managedObjectContext) as! Tube
newItems.adName = nameOfAdvert
newItems.thumbNail = thumbnail
newItems.smilURL = fileName
newItems.videoDownloadUrl = videoDownloadUrl
newItems.subTitle = subTitle
newItems.adType = adType
newItems.mainImage = mainImage
newItems.noOfLike = noOfLike
newItems.entryId = entryId
newItems.likeFlag = likeFlag
newItems.startDate = startDate
newItems.endDate = endDate
newItems.noOfCount = noOfCount
newItems.clientImage = clientImage
newItems.clientName = clientName
newItems.addescription = adDesc
newItems.sortDate = sortDate
newItems.newAdvertisement = newAdvertisement
newItems.seenAdvertisement = seenAdvertisement
do
try managedObjectContext.save()
catch let error as NSError
print("Could not save \(error), \(error.userInfo)")
print("-----------------------------")
print(newItems)
return newItems
【讨论】:
谢谢..我犯了这个大错。我在获取数据之前保存了数据...非常感谢你挽救了我的一天...以上是关于我的所有数据都没有存储在 Coredata的主要内容,如果未能解决你的问题,请参考以下文章
如何以索引方式有效地存储所有 OpenStreetMap 数据?
卸载 android 应用程序会删除存储在 sdcard 上的数据库吗?
为啥在使用 Knex.js/Objection 时我的所有数据都默认为 NULL?我根本没有通过 useNullAsDefault