adi adi - 1 month ago 9
iOS Question

My all data are not getting stored in Coredata

I'm fetching values from Server and storing them to Coredata.The issue is coming that when i fetch data from server and store them in coredata, it shows that i have stored my values all values.....

These are my data which are been stored:

[{
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";
}]


Now when i go to my .sqlite folder, which is at this location : /Users/Johnn/Library/Developer/CoreSimulator/Devices/0DF66BFE-6076-45E9-8325-D61F1C738DFA/data/Containers/Data/Application/DC3F7B17-1172-4AE3-BC3B-217624C21AA5/Documents/AppCoreData.sqlite

and open this .sqlite file> Go to my table i found:
enter image description here

that only two values are stored my last value of entry is empty.

I don't know what's the reason behind this,why my last value of array is not getting stored in database...

I am storing the values like this:

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
}


I am not knowing why all data are not getting saved in the coredata. Any help will be appreciated.

Thanks in advance

Answer

Please write this code :->

Writ "try managedObjectContext.save()" after you insert data.

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

}