How to edit Codable data model while saving it's data - ios

Hey I recently created released an app and I wanted to add some more features but the way that I saved the data of the app was in plists using Codable. The problem is that I want to add some more features and when I try to add or remove a variable in the data model it completely wipes the data. I really don't want my users to loose their data.
I have already tried making a new class that inherited the previous class, that stopped the data from being wiped but it didn't let me save anymore data. This is for an iOS app using the latest version of Swift and UIKit
my load data function
func loadClasses(){
if let data = try? Data(contentsOf: dataFilePath!){
let decoder = PropertyListDecoder()
do{
try classArray = decoder.decode([Course].self, from:
data)
}
catch{
print("error loading items \(error)")
}
}
if let data = try? Data(contentsOf: remindersDataFilePath!){
let decoder = PropertyListDecoder()
do{
try reminders = decoder.decode([Reminder].self, from:
data)
}
catch{
print("error loading items \(error)")
}
}
if let data = try? Data(contentsOf: completedDataFilePath!){
let decoder = PropertyListDecoder()
do{
try completedReminderes =
decoder.decode([Reminder].self, from: data)
}
catch{
print("error loading items \(error)")
}
}
if let data = try? Data(contentsOf: weightsDataFilePath!){
let decoder = PropertyListDecoder()
do{
try weights = decoder.decode(CourseWeights.self, from:
data)
print(weights.honors)
}
catch{
print("error loading items \(error)")
}
}
my save data function
func saveItems(){
let encoder = PropertyListEncoder()
do{
let data = try encoder.encode(classArray)
try data.write(to: dataFilePath!)
}catch{
print("Error encoding class array \(error)")
}
do{
let data = try encoder.encode(reminders)
try data.write(to: remindersDataFilePath!)
}catch{
print("Error encoding class array \(error)")
}
do{
let data = try encoder.encode(completedReminderes)
try data.write(to: completedDataFilePath!)
}catch{
print("Error encoding class array \(error)")
}
do{
let data = try encoder.encode(weights)
try data.write(to: weightsDataFilePath!)
}catch{
print("Error encoding weights array \(error)")
}
if(reminders.count>0){
for i in 0...reminders.count-1{
if(reminders.count > 0){
scheduleLocal(remind: reminders[i])
}
}
}
}
the data model I want to add more to
class oldReminder : Codable{
var name : String = "Untitled"
var completed : Bool = false
var willNotify : Bool = false
var due = Date()
var reminder = Date()
var course = Course()
let uuid = UUID().uuidString
}

Related

How to load a JSON tableView and save data even on dismiss?

I currently have a button that opens a TableViewController and loads the data using JSON like the following:
private func JSON() {
print(facility)
guard let url = URL(string: "https://example/example/example"),
let sample = value1.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)
else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = "example1=\(example)".data(using: .utf8)
URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data else { return }
do {
self.JStruct = try JSONDecoder().decode([exampleStruct].self,from:data)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
catch {
print(error)
}
}.resume()
}
Then after I am done looking at the tableview I close it by doing:
self.dismiss(animated: true, completion: nil)
using a BarButtonItem.
The issue is every time the UIView opens it takes some time to load the data. Is there anyway to have the tableView load just once and when dismissed and re-opened just have the same data show that was already loaded before?
The best thing you can do is to store the data locally. Either use a local database or a plain text file to store the data. When you open the page check whether data is already present. If it is already present load it, and call the API in background silently to update the existing data. If data is not saved, call the API, load the data and save it locally.
func getFileURL() -> URL {
let fileName = "CacheData"
let documentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = documentDirURL.appendingPathComponent(fileName).appendingPathExtension("json")
return fileURL
}
func createFile(data: Data) {
let fileURL = getFileURL()
do {
try data.write(to: fileURL)
} catch let e {
print(e.localizedDescription)
}
}
func loadData() -> Data? {
let fileURL = getFileURL()
do {
let data = try Data(contentsOf: fileURL)
return data
} catch let e {
print(e.localizedDescription)
}
return nil
}
In your viewDidLoad method do something like:
let fileURL = getFileURL()
if FileManager.default.fileExists(atPath: fileURL.path) {
if let data = loadData() {
do {
self.JStruct = try
JSONDecoder().decode([exampleStruct].self,from:data)
DispatchQueue.main.async {
self.tableView.reloadData()
} catch {
print(error)
}
}
}
JSON()
And call the createFile when you get data from the API. You may need to write the file and load the file using a background queue to avoid overloading and freezing of your main thread.

Contact image not getting when fetch all contact list from iPhone by CNContact

I know this question already asked but not getting solution.
From this code I will get all the information from the contact but image not found when open vcf files on mac os, also not getting when share this file. I use this stackoverflow link here but It's not help full.
var contacts = [CNContact]()
let keys = [CNContactVCardSerialization.descriptorForRequiredKeys()
] as [Any]
let request = CNContactFetchRequest(keysToFetch: keys as! [CNKeyDescriptor])
do {
try self.contactStore.enumerateContacts(with: request) {
(contact, stop) in
// Array containing all unified contacts from everywhere
contacts.append(contact)
}
} catch {
print("unable to fetch contacts")
}
do {
let data = try CNContactVCardSerialization.data(with: contacts)
if let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = directoryURL.appendingPathComponent("contacts").appendingPathExtension("vcf")
print(fileURL)
do {
try data.write(to: fileURL, options: .atomic)
} catch {
print("error \(error)")
}
}
} catch {
print("error \(error)")
}
Probably,
let data = try CNContactVCardSerialization.data(with: contacts)
Only adds the contact info without image tag, and hence you need to add image tag manually into your VCF file. you can find the solution here.
https://stackoverflow.com/a/44308365/5576675
Yes,
let data = try CNContactVCardSerialization.data(with: contacts)
give only contacts info not image data so you need to do like this, you can get correct VCF files.
var finalData = Data()
for contact in contacts {
do {
var data = try CNContactVCardSerialization.data(with: [contact])
var vcString = String(data: data, encoding: String.Encoding.utf8)
let base64Image = contact.imageData?.base64EncodedString()
let vcardImageString = "PHOTO;TYPE=JPEG;ENCODING=BASE64:" + (base64Image ?? "") + ("\n")
vcString = vcString?.replacingOccurrences(of: "END:VCARD", with: vcardImageString + ("END:VCARD"))
data = (vcString?.data(using: .utf8))!
finalData += data
} catch {
print("error \(error)")
}
}
if let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = directoryURL.appendingPathComponent("contacts").appendingPathExtension("vcf")
do {
try finalData.write(to: fileURL, options: .atomic)
} catch {
print("error \(error)")
}
}

Preload database with CoreData, Swift 4

I try to preload my database in my app (.sqlite, .sqlite-wal and .sqlite-shm), but I can't.
I try this:
func preloadDBData()
{
let sqlitePath = Bundle.main.path(forResource: "leksikonPreload", ofType: "sqlite")
let sqlitePath_shm = Bundle.main.path(forResource: "leksikonPreload", ofType: "sqlite-shm")
let sqlitePath_wal = Bundle.main.path(forResource: "leksikonPreload", ofType: "sqlite-wal")
let URL1 = URL(fileURLWithPath: sqlitePath!)
let URL2 = URL(fileURLWithPath: sqlitePath_shm!)
let URL3 = URL(fileURLWithPath: sqlitePath_wal!)
let URL4 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/leksikonPreload.sqlite")
let URL5 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/leksikonPreload.sqlite-shm")
let URL6 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/leksikonPreload.sqlite-wal")
if !FileManager.default.fileExists(atPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/leksikonPreload.sqlite") {
// Copy 3 files
do {
try FileManager.default.copyItem(at: URL1, to: URL4)
try FileManager.default.copyItem(at: URL2, to: URL5)
try FileManager.default.copyItem(at: URL3, to: URL6)
print("=======================")
print("FILES COPIED")
print("=======================")
} catch {
print("=======================")
print("ERROR IN COPY OPERATION")
print("=======================")
}
} else {
print("=======================")
print("FILES EXIST")
print("=======================")
}
}
and call preloadDBData() in my appDelegate didFinishLaunchingWithOptions method. Name of my Core Data model: "leksikon", name of my preload data: "leksikonPreload". This func work fine and really write when "FILES COPYED" and "FILES EXIST", but when I try print my data - my array contain 0 elements, but I am sure, that leksikonPreload contains 12 values.
My code of print:
func printData() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetchRequest: NSFetchRequest<Word> = Word.fetchRequest()
do
{
let arr = try context.fetch(fetchRequest)
for obj in arr {
print(obj.engValue)
print(obj.rusValue)
print(obj.was)
}
}
catch
{
print(error.localizedDescription)
}
}
I am having the same problem.
I have a preloadedDataBase called: BenedictusCoreData.sqlite
I added it to my project. And modified the AppDelegate:
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "BenedictusCoreData")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Preload data from my sqlite file.
func preloadDBData() {
let sqlitePath = Bundle.main.path(forResource: "BenedictusCoreData", ofType: "sqlite")
let originURL = URL(fileURLWithPath: sqlitePath!)
let destinationURL = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/BenedictusCoreData.sqlite")
if !FileManager.default.fileExists(atPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/BenedictusCoreData.sqlite") {
// Copy file
do {
try FileManager.default.copyItem(at: originURL, to: destinationURL)
print("=======================")
print("FILE COPIED")
print("=======================")
} catch {
print("=======================")
print("ERROR IN COPY OPERATION")
print("=======================")
}
} else {
print("=======================")
print("FILE EXIST")
print("=======================")
}
}
I copied only the sqlite file because I have no wal and shm files and because I just want to copy the file the first time it is launched and only for reading purposes.
The preloadDBData() function is called in AppDelegate didFinishLaunchingWithOptions. And when launched for the first time says "FILE COPIED". And after it, when launched said "FILE EXIST". So it seems that the file is copied.
The model file in the project is called BenedictusCoreData.xcdatamodeld and the project name is BenedictusCoreData
And in the ViewController I wrote the following:
var encyclicalsArray = [Encyclicals]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadItems()
}
func loadItems() {
let request: NSFetchRequest<Encyclicals> = Encyclicals.fetchRequest()
do {
try encyclicalsArray = context.fetch(request)
for encyclical in encyclicalsArray {
print(encyclical.titulo_es!)
print(encyclical.date!)
print(encyclical.idURL!)
}
} catch {
print("Error fetching encyclicals: \(error)")
}
}
And it prints nothing. However, my sqlite preloaded file has three entries within this table.

File path saved to CoreData but NSData works only on first call

I'm downloading a picture from internet and storing its data locally then saving the path in my CoreData, this way:
getDataFromUrl(url!) { (data, response, error) in
dispatch_async(dispatch_get_main_queue()) { () -> Void in
guard let data = data where error == nil else { return }
print(response?.suggestedFilename ?? "")
print("Download Finished")
let filename = self.getDocumentsDirectory().stringByAppendingPathComponent(userKey as! String + ".png")
data.writeToFile(filename, atomically: true)
user.setValue(filename, forKey: "avatar")
do {
try managedContext.save()
}
catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
}
The save does seem to work (I debugged by printing the data received and the data inside the file once copied and I don't have any managedContext error).
On the next view, I do use a UITableView and on cellForRowAtIndexPath
let path = authorArray.objectAtIndex(indexPath.row).objectAtIndex(0).objectForKey("avatar")! as! String
let name = authorArray.objectAtIndex(indexPath.row).objectAtIndex(0).objectForKey("name")
do {
let data = try NSData(contentsOfFile: path, options: NSDataReadingOptions())
let image = UIImage(data: data)
cell.profilePicture.image = image
cell.profilePicture.layer.cornerRadius = cell.profilePicture.layer.cornerRadius / 2;
cell.profilePicture.layer.masksToBounds = true;
}
catch {
print("failed pictures")
}
The thing is I get the photo on my cell.profilePicture but as soon as I do any modification elsewhere and relaunch my application from xCode, I get the error message. The pictures path did not change but the datas obtained from it are nil. I can't find a reason why it does work until I update the code. Any solutions to make it work everytime ?
As pbasdf stated on comments, I was storing the whole Document directory path instead of just the filename + extension. Documents directory changes on every build.

Reading and saving data into a file [duplicate]

I already have read Read and write data from text file
I need to append the data (a string) to the end of my text file.
One obvious way to do it is to read the file from disk and append the string to the end of it and write it back, but it is not efficient, especially if you are dealing with large files and doing in often.
So the question is "How to append string to the end of a text file, without reading the file and writing the whole thing back"?
so far I have:
let dir:NSURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.CachesDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last as NSURL
let fileurl = dir.URLByAppendingPathComponent("log.txt")
var err:NSError?
// until we find a way to append stuff to files
if let current_content_of_file = NSString(contentsOfURL: fileurl, encoding: NSUTF8StringEncoding, error: &err) {
"\(current_content_of_file)\n\(NSDate()) -> \(object)".writeToURL(fileurl, atomically: true, encoding: NSUTF8StringEncoding, error: &err)
}else {
"\(NSDate()) -> \(object)".writeToURL(fileurl, atomically: true, encoding: NSUTF8StringEncoding, error: &err)
}
if err != nil{
println("CANNOT LOG: \(err)")
}
Here's an update for PointZeroTwo's answer in Swift 3.0, with one quick note - in the playground testing using a simple filepath works, but in my actual app I needed to build the URL using .documentDirectory (or which ever directory you chose to use for reading and writing - make sure it's consistent throughout your app):
extension String {
func appendLineToURL(fileURL: URL) throws {
try (self + "\n").appendToURL(fileURL: fileURL)
}
func appendToURL(fileURL: URL) throws {
let data = self.data(using: String.Encoding.utf8)!
try data.append(fileURL: fileURL)
}
}
extension Data {
func append(fileURL: URL) throws {
if let fileHandle = FileHandle(forWritingAtPath: fileURL.path) {
defer {
fileHandle.closeFile()
}
fileHandle.seekToEndOfFile()
fileHandle.write(self)
}
else {
try write(to: fileURL, options: .atomic)
}
}
}
//test
do {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url = dir.appendingPathComponent("logFile.txt")
try "Test \(Date())".appendLineToURL(fileURL: url as URL)
let result = try String(contentsOf: url as URL, encoding: String.Encoding.utf8)
}
catch {
print("Could not write to file")
}
Thanks PointZeroTwo.
You should use NSFileHandle, it can seek to the end of the file
let dir:NSURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.CachesDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last as NSURL
let fileurl = dir.URLByAppendingPathComponent("log.txt")
let string = "\(NSDate())\n"
let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
if NSFileManager.defaultManager().fileExistsAtPath(fileurl.path!) {
var err:NSError?
if let fileHandle = NSFileHandle(forWritingToURL: fileurl, error: &err) {
fileHandle.seekToEndOfFile()
fileHandle.writeData(data)
fileHandle.closeFile()
}
else {
println("Can't open fileHandle \(err)")
}
}
else {
var err:NSError?
if !data.writeToURL(fileurl, options: .DataWritingAtomic, error: &err) {
println("Can't write \(err)")
}
}
A variation over some of the posted answers, with following characteristics:
based on Swift 5
accessible as a static function
appends new entries to the end of the file, if it exists
creates the file, if it doesn't exist
no cast to NS objects (more Swiftly)
fails silently if the text cannot be encoded or the path does not exist
class Logger {
static var logFile: URL? {
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil }
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
let dateString = formatter.string(from: Date())
let fileName = "\(dateString).log"
return documentsDirectory.appendingPathComponent(fileName)
}
static func log(_ message: String) {
guard let logFile = logFile else {
return
}
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss"
let timestamp = formatter.string(from: Date())
guard let data = (timestamp + ": " + message + "\n").data(using: String.Encoding.utf8) else { return }
if FileManager.default.fileExists(atPath: logFile.path) {
if let fileHandle = try? FileHandle(forWritingTo: logFile) {
fileHandle.seekToEndOfFile()
fileHandle.write(data)
fileHandle.closeFile()
}
} else {
try? data.write(to: logFile, options: .atomicWrite)
}
}
}
Here is a way to update a file in a much more efficient way.
let monkeyLine = "\nAdding a šŸµ to the end of the file via FileHandle"
if let fileUpdater = try? FileHandle(forUpdating: newFileUrl) {
// Function which when called will cause all updates to start from end of the file
fileUpdater.seekToEndOfFile()
// Which lets the caller move editing to any position within the file by supplying an offset
fileUpdater.write(monkeyLine.data(using: .utf8)!)
// Once we convert our new content to data and write it, we close the file and thatā€™s it!
fileUpdater.closeFile()
}
Here's a version for Swift 2, using extension methods on String and NSData.
//: Playground - noun: a place where people can play
import UIKit
extension String {
func appendLineToURL(fileURL: NSURL) throws {
try self.stringByAppendingString("\n").appendToURL(fileURL)
}
func appendToURL(fileURL: NSURL) throws {
let data = self.dataUsingEncoding(NSUTF8StringEncoding)!
try data.appendToURL(fileURL)
}
}
extension NSData {
func appendToURL(fileURL: NSURL) throws {
if let fileHandle = try? NSFileHandle(forWritingToURL: fileURL) {
defer {
fileHandle.closeFile()
}
fileHandle.seekToEndOfFile()
fileHandle.writeData(self)
}
else {
try writeToURL(fileURL, options: .DataWritingAtomic)
}
}
}
// Test
do {
let url = NSURL(fileURLWithPath: "test.log")
try "Test \(NSDate())".appendLineToURL(url)
let result = try String(contentsOfURL: url)
}
catch {
print("Could not write to file")
}
In order to stay in the spirit of #PointZero Two.
Here an update of his code for Swift 4.1
extension String {
func appendLine(to url: URL) throws {
try self.appending("\n").append(to: url)
}
func append(to url: URL) throws {
let data = self.data(using: String.Encoding.utf8)
try data?.append(to: url)
}
}
extension Data {
func append(to url: URL) throws {
if let fileHandle = try? FileHandle(forWritingTo: url) {
defer {
fileHandle.closeFile()
}
fileHandle.seekToEndOfFile()
fileHandle.write(self)
} else {
try write(to: url)
}
}
}
Update: I wrote a blog post on this, which you can find here!
Keeping things Swifty, here is an example using a FileWriter protocol with default implementation (Swift 4.1 at the time of this writing):
To use this, have your entity (class, struct, enum) conform to this protocol and call the write function (fyi, it throws!).
Writes to the document directory.
Will append to the text file if the file exists.
Will create a new file if the text file doesn't exist.
Note: this is only for text. You could do something similar to write/append Data.
import Foundation
enum FileWriteError: Error {
case directoryDoesntExist
case convertToDataIssue
}
protocol FileWriter {
var fileName: String { get }
func write(_ text: String) throws
}
extension FileWriter {
var fileName: String { return "File.txt" }
func write(_ text: String) throws {
guard let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
throw FileWriteError.directoryDoesntExist
}
let encoding = String.Encoding.utf8
guard let data = text.data(using: encoding) else {
throw FileWriteError.convertToDataIssue
}
let fileUrl = dir.appendingPathComponent(fileName)
if let fileHandle = FileHandle(forWritingAtPath: fileUrl.path) {
fileHandle.seekToEndOfFile()
fileHandle.write(data)
} else {
try text.write(to: fileUrl, atomically: false, encoding: encoding)
}
}
}
All answers (as of now) recreate the FileHandle for every write operation. This may be fine for most applications, but this is also rather inefficient: A syscall is made, and the filesystem is accessed each time you create the FileHandle.
To avoid creating the filehandle multiple times, use something like:
final class FileHandleBuffer {
let fileHandle: FileHandle
let size: Int
private var buffer: Data
init(fileHandle: FileHandle, size: Int = 1024 * 1024) {
self.fileHandle = fileHandle
self.size = size
self.buffer = Data(capacity: size)
}
deinit { try! flush() }
func flush() throws {
try fileHandle.write(contentsOf: buffer)
buffer = Data(capacity: size)
}
func write(_ data: Data) throws {
buffer.append(data)
if buffer.count > size {
try flush()
}
}
}
// USAGE
// Create the file if it does not yet exist
FileManager.default.createFile(atPath: fileURL.path, contents: nil)
let fileHandle = try FileHandle(forWritingTo: fileURL)
// Seek will make sure to not overwrite the existing content
// Skip the seek to overwrite the file
try fileHandle.seekToEnd()
let buffer = FileHandleBuffer(fileHandle: fileHandle)
for i in 0..<count {
let data = getData() // Your implementation
try buffer.write(data)
print(i)
}

Resources