saoudrizwan / disk Goto Github PK
View Code? Open in Web Editor NEWDelightful framework for iOS to easily persist structs, images, and data
License: MIT License
Delightful framework for iOS to easily persist structs, images, and data
License: MIT License
When trying to save a UIImage, the app crashes and I get this error inside of the library files
Thread 1: Fatal error: Optional does not conform to Encodable because UIImage does not conform to Encodable.
Anyone tried using Mapper (https://github.com/lyft/mapper) with Disk? Basically the models should have Mappable protocol for using Mapper but whenever I add Codable beside Mappable I get an error "Type Model does not conform to protocol Decodable", "Type Model does not conform to protocol Encodable"
use serial DispatchQueue to dispatch IO task will be better ?
I'm saving files in the documents directory and expected them to be automatically backed up to iCloud (and restored after app deletion and new install).
I also enabled the iCloud entitlement in the app capabilities but it still doesn't work.
Tried to enable backup to iCloud (if it was somehow disabled) by using Disk.backup(filename, in: .documents)
but no luck here either.
Do you have any ideas what could cause this behavior?
(yes, I'm logged in with my Apple ID)
It is currently relatively slow to save many images to a folder. Using the new codable protocol, wouldn't it be a lot faster to convert the uiimage to base64, then save it as json, and then convert it back when retrieving it? It would be cool to have this option.
Hey,
After using disk I am getting below warnings so please let me know how to get rid of the warnings.
/Users/faiyyazkhatri/Documents/IntroApp/Pods/Disk/Sources/Disk+Helpers.swift:34:6: Unexpected version number in 'available' attribute for non-specific platform '*'
/Users/faiyyazkhatri/Documents/IntroApp/Pods/Disk/Sources/Disk+VolumeInformation.swift:39:5: 'public' modifier is redundant for static property declared in a public extension
/Users/faiyyazkhatri/Documents/IntroApp/Pods/Disk/Sources/Disk+VolumeInformation.swift:47:5: 'public' modifier is redundant for static property declared in a public extension
/Users/faiyyazkhatri/Documents/IntroApp/Pods/Disk/Sources/Disk+VolumeInformation.swift:58:5: 'public' modifier is redundant for static property declared in a public extension
/Users/faiyyazkhatri/Documents/IntroApp/Pods/Disk/Sources/Disk+VolumeInformation.swift:73:5: 'public' modifier is redundant for static property declared in a public extension
Hi!.
This is not really an issue it is more a doubt, Please help.
Im using the following code to save a codable class:
try! Disk.save(self.order, to: .caches, as: Constants.PEDIDO_FOLDER)
Where self.order is the codable class and Constants.PEDIDO_FOLDER is "VASH/Pedido.json"
And when a want to have the memory of the codable class i do it this way:
self.pedido = try Disk.retrieve(Constants.PEDIDO_FOLDER, from: .caches, as: Order.self)
This is working perfectly!. But if i leave the app like 30 minutes inactived and then i reopen the app. The caches memory is not longer there. My doubt is if there is a way to have the memory for a longer time in caches.
Thanks a lot!.
Hi @saoudrizwan, thank you for the cool library! Very useful.
As I am not thinking to use it wisely in my app, I would like to check, if you're offering search/filter the data while fetching it self?
try Disk.clear(.caches)
Works in the simulator, but on my device all I get is an error:
"Snapshots" couldn't be removed because you don't have permission to access it.
Save/retrieve works on the device.
iOS 11.1.2 (15B202)
What am I doing wrong? Do I need to ask for some sort of permissions?
Edit: Did a bit of further digging:
https://stackoverflow.com/questions/12488462/ios-directory-structure-whats-snapshots-file
The Snapshots file/folder exists both in the simulator and on the actual device (phone).
The difference is that on the simulator it gets deleted, on the phone I get the error message...
I've saved an object with
try! Disk.save(recipe, to: .documents, as: "Recipes/"+recipe.fileName+".json")
and tried to load it with
try! Disk.retrieve("Recipes", from: .documents, as: [Recipe].self)
and got the following error:
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=257 "The file “Ingredients” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/Users/danyel/Library/Developer/CoreSimulator/Devices/9D629C11-EC8B-4B0D-A5FD-1D2FC8B1D5F5/data/Containers/Data/Application/A55E48FA-C5C6-4796-8A32-946E51313879/Documents/Ingredients, NSUnderlyingError=0x60400024d650 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}
Any ideas for a solution?
Is there a reason why in the Readme you're only talking about handling structs, but not classes?
In quick tests saving and retrieving codable class instances works fine.
Will there be any changes in class support?
I'm asking because I want to use your awesome library for my project but I have some models that are class-based.
Would it be possible to add tvOS support?
I first created an empty Xcode Project with swift init
Then edited the Package.swift to include the Disk Dependency and Disk target
After that run on Root Folder of project from terminal swift package generate-xcodeproj
Then swift build
throws this errors:
Compile Swift Module 'Disk' (10 sources) /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:33:31: error: use of undeclared type 'UIImage' static func save(_ value: UIImage, to directory: Directory, as path: String) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:33:32: error: use of undeclared type 'UIImage' static func save(_ value: [UIImage], to directory: Directory, as path: String) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:88:102: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: UIImage.Type) throws -> UIImage { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:88:78: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: UIImage.Type) throws -> UIImage { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:141:105: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: [UIImage].Type) throws -> [UIImage] { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:141:79: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: [UIImage].Type) throws -> [UIImage] { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:33:31: error: use of undeclared type 'UIImage' static func save(_ value: UIImage, to directory: Directory, as path: String) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:33:32: error: use of undeclared type 'UIImage' static func save(_ value: [UIImage], to directory: Directory, as path: String) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:71:33: error: use of undeclared type 'UIImage' static func append(_ value: UIImage, to path: String, in directory: Directory) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:119:34: error: use of undeclared type 'UIImage' static func append(_ value: [UIImage], to path: String, in directory: Directory) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:88:102: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: UIImage.Type) throws -> UIImage { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:88:78: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: UIImage.Type) throws -> UIImage { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:141:105: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: [UIImage].Type) throws -> [UIImage] { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:141:79: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: [UIImage].Type) throws -> [UIImage] { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:60:64: error: 'volumeAvailableCapacityForImportantUsageKey' is only available on OS X 10.13 or newer let resourceValues = getVolumeResourceValues(for: .volumeAvailableCapacityForImportantUsageKey) ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:60:64: note: add 'if #available' version check let resourceValues = getVolumeResourceValues(for: .volumeAvailableCapacityForImportantUsageKey) ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:58:23: note: add @available attribute to enclosing static var public static var availableCapacityForImportantUsage: Int? { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:30:8: note: add @available attribute to enclosing extension public extension Disk { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:61:45: error: 'volumeAvailableCapacityForImportantUsage' is only available on OS X 10.13 or newer if let result = resourceValues?.volumeAvailableCapacityForImportantUsage { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:61:45: note: add 'if #available' version check if let result = resourceValues?.volumeAvailableCapacityForImportantUsage { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:58:23: note: add @available attribute to enclosing static var public static var availableCapacityForImportantUsage: Int? { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:30:8: note: add @available attribute to enclosing extension public extension Disk { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:75:64: error: 'volumeAvailableCapacityForOpportunisticUsageKey' is only available on OS X 10.13 or newer let resourceValues = getVolumeResourceValues(for: .volumeAvailableCapacityForOpportunisticUsageKey) ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:75:64: note: add 'if #available' version check let resourceValues = getVolumeResourceValues(for: .volumeAvailableCapacityForOpportunisticUsageKey) ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:73:23: note: add @available attribute to enclosing static var public static var availableCapacityForOpportunisticUsage: Int? { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:30:8: note: add @available attribute to enclosing extension public extension Disk { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:76:45: error: 'volumeAvailableCapacityForOpportunisticUsage' is only available on OS X 10.13 or newer if let result = resourceValues?.volumeAvailableCapacityForOpportunisticUsage { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:76:45: note: add 'if #available' version check if let result = resourceValues?.volumeAvailableCapacityForOpportunisticUsage { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:73:23: note: add @available attribute to enclosing static var public static var availableCapacityForOpportunisticUsage: Int? { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+VolumeInformation.swift:30:8: note: add @available attribute to enclosing extension public extension Disk { ^ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:33:31: error: use of undeclared type 'UIImage' static func save(_ value: UIImage, to directory: Directory, as path: String) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:33:32: error: use of undeclared type 'UIImage' static func save(_ value: [UIImage], to directory: Directory, as path: String) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:71:33: error: use of undeclared type 'UIImage' static func append(_ value: UIImage, to path: String, in directory: Directory) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:119:34: error: use of undeclared type 'UIImage' static func append(_ value: [UIImage], to path: String, in directory: Directory) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:88:102: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: UIImage.Type) throws -> UIImage { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:88:78: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: UIImage.Type) throws -> UIImage { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:141:105: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: [UIImage].Type) throws -> [UIImage] { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:141:79: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: [UIImage].Type) throws -> [UIImage] { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:33:31: error: use of undeclared type 'UIImage' static func save(_ value: UIImage, to directory: Directory, as path: String) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:88:102: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: UIImage.Type) throws -> UIImage { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:88:78: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: UIImage.Type) throws -> UIImage { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:33:32: error: use of undeclared type 'UIImage' static func save(_ value: [UIImage], to directory: Directory, as path: String) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:71:33: error: use of undeclared type 'UIImage' static func append(_ value: UIImage, to path: String, in directory: Directory) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:119:34: error: use of undeclared type 'UIImage' static func append(_ value: [UIImage], to path: String, in directory: Directory) throws { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:141:105: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: [UIImage].Type) throws -> [UIImage] { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+[UIImage].swift:141:79: error: use of undeclared type 'UIImage' static func retrieve(_ path: String, from directory: Directory, as type: [UIImage].Type) throws -> [UIImage] { ^~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:37:31: error: use of unresolved identifier 'UIImagePNGRepresentation' if let data = UIImagePNGRepresentation(value) { ^~~~~~~~~~~~~~~~~~~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:48:31: error: use of unresolved identifier 'UIImageJPEGRepresentation' if let data = UIImageJPEGRepresentation(value, 1) { ^~~~~~~~~~~~~~~~~~~~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:59:31: error: use of unresolved identifier 'UIImagePNGRepresentation' if let data = UIImagePNGRepresentation(value) { ^~~~~~~~~~~~~~~~~~~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:61:38: error: use of unresolved identifier 'UIImageJPEGRepresentation' } else if let data = UIImageJPEGRepresentation(value, 1) { ^~~~~~~~~~~~~~~~~~~~~~~~~ /Users/fran/Desktop/TestApp/.build/checkouts/Disk.git-5746674856174794633/Sources/Disk+UIImage.swift:92:28: error: use of unresolved identifier 'UIImage'; did you mean 'CGImage'? if let image = UIImage(data: data) { ^~~~~~~ CGImage CoreGraphics.CGImage:1:14: note: 'CGImage' declared here public class CGImage : _CFObject { ^ error: terminated(1): /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/fran/Desktop/TestApp/.build/debug.yaml main output:
This library is awesome- lightweight, fun, and useful. Thank you for making this!
Have you thought about any kind of data migration? For example, if someone adds/removes a property to a struct after saving it, and then tries to load a version of a json file saved using the old version of the stuct?
Guessing you probably don't want to build something as robust as Realm's schema migration... have you brainstormed ideas on the best way to implement this?
Thanks again!
I think we need to pass encoder & decoder if necessary.
A pull request is here #16
Hi @saoudrizwan , is it possible to share the data between the iOS app and watchOS app?
Hello @saoudrizwan! Thank you for sharing Disk, I am excited to try it out. I'm running into a small issue I've tried to debug but I'm not having any luck. Maybe you can help.
I shortened the code to the smallest reproducible case. With this code...
struct Message: Codable {
var body: String
}
static func saveMessage() {
do {
let messages = [Message]()
try Disk.save(messages, to: .documents, as: "Messages/")
let first = Message(body: "Hello world.")
try Disk.save(first, to: .documents, as: "Messages/first.json")
} catch let error as NSError {
print(error)
}
}
I get this error...
Error Domain=NSCocoaErrorDomain Code=516 "The file “Messages” couldn’t be saved
in the folder “Documents” because a file with the same name already exists."
UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/F45ABDA8-33A7-4003-9552-66722A855C55/Documents/Messages,
NSUnderlyingError=0x1c024d500 {Error Domain=NSPOSIXErrorDomain Code=17 "File exists"}}
Things I've tried:
My end goal is to have a folder with individual JSON files, one for each Codable
. From the readme, it seems that the one possible set of steps is:
[Codable]
Codable
type with a path that is [folderName]/[fileName].json
Am I perhaps using Disk incorrectly? Thank you for your help!
Below class only get saved into Disk if CodingKeys are removed
class Agendamento: Codable{
var response: String?
var serialNumber: String?
var nome: String?
var ativo = false
var programacoes = [Programacao]()
init(nome: String, ativo: Bool = false){
self.nome = nome
self.ativo = ativo
self.programacoes = [Programacao]()
}
init(){}
private enum CodingKeys: String, CodingKey {
case response = "RES"
case serialNumber = "SN"
case programacoes = "P"
}
}
Hello there,
will you support videos ?
Hi,
How can I delete a file saved with Disk?
Thanks!
Hi,
First, great work! Second, I have a realm database file, which I would like to share with app extensions and also persist it even in low storage situations. Currently the data is being purged because of low space.
I have not found any mention about it yet, but do you know if will the method .doNotBackup
work on such files?
I am currently using
let path = "\(getDatabaseName()).realm"
let fileURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: group)!.appendingPathComponent(caminho)
let configuration = Realm.Configuration(fileURL: fileURL, encryptionKey: getKey() as Data)
configuration.deleteRealmIfMigrationNeeded = true
let realm = try Realm(configuration: configuration)
do { try Disk.doNotBackup(path, in: .sharedContainer(appGroupName: group))
} catch let error as NSError {
debugPrint(error.localizedDescription) }`
None error is thrown, however I am not sure if this will work since you have mentioned this method only for .documents
and . applicationSupport
, or if it is even the best approach (suggestions?).
It is in my plans to implement a check where I prevent the user from using the app, or at least warn him, if the available space is too low using Disk.availableCapacity
. Nevertheless, are there other implications besides prevention from being backed up by iCloud or iTunes? Anything I should be worried, I mean.
The project does not compile with Xcode 10.2 and Swift 5.
Implemente the necessary changes to make it compatible with Swift 5
Are there plans to support reading files from app's bundle? thanks.
Save data to App Group is a thing.
Do we need a new case in Disk.Directory?
Apologies if this is supported already - I have only reads the reader so far.
Is there a command for appending to a file? Or should we read the file first back into model objects, append objects, and rewrite?
Similarly, can we remove the contents of a file without removing the file itself? Or should we delete it and recreate it with an empty array for example?
Hi. A curious question, perhaps?
I have adapted Disk to work in my macOS app project except for the UIImage framework files.
I would like to cache some book cover preview images that were loaded into a collection view.
I've been looking at NSImage repos on GitHub too, and found ESSImageCategory, which allows
NSImages to be written to disk in other formats other than just TIFF. It's in Objective-C, but I'll
look it over and see if I can borrow ideas from the implementation of representationForFileType.
Any suggestions or help in converting the UIImage files will be awesome, thanks.
Cheers
--mike
When I want to pass encoder with custom nonConformingFloatEncodingStrategy to Disk.append, it will not get passed inside the function to the Disk.save function. The encoder parameter is there but it has a default value and is not overwritten.
I think this should be added.
Say we start with some model user:
class User {
enum CodingKeys: String, CodingKey {
case name = "name"
}
var name: String
}
And later we add a property called like birthdate: Date
In Disk, we are faced with two choices... We can omit birthdate
from the CodingKeys enum forever and provide a default value for all user's birthdates (not ideal), or we can add birthdate
to the keys and lose all previously stored User
files (also not ideal).
Is there some way to get the NSKeyArchiver to fall back and not abort altogether? Like, if in the event that the model DOES provide a default value for a property, it could maybe not absolutely require all of the CodingKeys.
Use case: I use Disk with this protocol to quickly store singleton objects with settings/preferences on them. Currently, if I "add" a preference property to one of these singletons, all of the previous settings get wiped out. Just curious if this is a reasonable feature in the scope of Disk.
public protocol Persistable: Codable {
init()
static func load() -> Self
static func save(_ obj: Self)
static var persistenceFileName: String { get }
}
extension Persistable {
public static func load() -> Self {
if let manager = try? Disk.retrieve(Self.persistencePath, from: .documents, as: Self.self) {
return manager
} else {
return Self()
}
}
public static func save(_ obj: Self) {
try? Disk.save(obj, to: .documents, as: Self.persistencePath)
}
public static var persistencePath: String {
return self.persistenceFileName.appending(".json")
}
}
Avoid duplicating Equatable or Hashable objects. Replace existing object instead.
Hi @saoudrizwan , I am a long time user of this library! Thank you. ✌️Today, I came across a crash is happening when I will add a new variable to my struct
.
Example:
struct
named: Person. I will store name and age details of a person.struct Person: Codable {
var name: String = ""
var age: Int = 30
}
Now, I will create few objects of Person
and will save it using Disk.
Later, I have a new requirements and I need to add one more variable to store a person's gender
. So I'll update my struct like this:
struct Person: Codable {
var name: String = ""
var age: Int = 30
var gender: String = "M"
}
The only solution is to empty Person.json
file and run the app again.
Anyways to fix this?
Hi I'm using this library in my application and it's wonderful.
Just wondering can we have expiry setting for objects that are added to cache.
Thanks.
I'm getting a permissions error when trying to save data to a subfolder that is created as part of the save.
Here's the code where I'm trying to save the data:
do {
let data = try Data(contentsOf: url)
let path = "Inbox/PDF/\(title)"
try Disk.save(data, to: .documents, as: path)
} catch let error as NSError {
print("""
Domain: \(error.domain)
Code: \(error.code)
Description: \(error.localizedDescription)
Failure Reason: \(error.localizedFailureReason ?? "")
Suggestions: \(error.localizedRecoverySuggestion ?? "")
""")
}
When I run this on the simulator, it works just fine. However, when I run it on a device, I get this response:
Domain: NSCocoaErrorDomain
Code: 513
Description: You don’t have permission to save the file “PDF” in the folder “Inbox”.
Failure Reason: You don’t have permission.
Suggestions: To view or change permissions, select the item in the Finder and choose File > Get Info.
I tracked it down, and the error is bubbling up from Disk+InternalHelpers.swift::188
. That line calls createDirectory(at:withIntermediateDirectories:attributes:)
, which is throwing the error. The really weird thing is that "PDF" is supposed to be a subfolder of "Inbox", not a file inside it.
Here's the value for the subfolderUrl
variable:
file:///var/mobile/Containers/Data/Application/AC2C2E56-8188-426A-8C59-E508B56C7954/Documents/Inbox/PDF/
Am I doing something wrong here? Why would this work in the simulator, but throw on a device?
Hi @saoudrizwan , thanks for making this library! I'm also using Disk very extensively as a backend of my app!
Here's the app link: https://itunes.apple.com/gb/app/nastromy/id1444105372?mt=8
Here's the Github page: https://github.com/hemangshah/Nastromy
Hope to see this on the app using Disk list too! :)
I saved the image using Disk.save(image, to: .documents, as: "CustomAlbum/filename.jpg") but how will it be saved to the actual photo library? its only stored in app's data is there a way to also save it onto the actual device also?
I've tried to save an image as shown in your example, and the app always crashes with,
fatal error: Optional does not conform to Encodable because UIImage does not conform to Encodable
When I hold down Alt/Option and hover over my declaration, it shows the declaration for the Codable initializer even though I've used Disk.save with an UIImage.
Any ideas if I am using it wrong?
Please consider propagating errors to the user so that they can be handled at runtime.
Hi!
I have a use case where I would like to check wether a folder is empty or not.
As of today, I use this:
var hasContent: Bool {
guard let data = try? Disk.retrieve(folder.name, from: directory, as: [Data].self) else { return false }
return data.count > 0
}
However this does not seem very performant. Is there possibility for a simple "folderIsEmpty" or such? We are storing data in a subfolder within the directory.
Maybe I'm missing something. And also, I'm not sure wether this should be posted as an issue at all. :)
Hi. I love this framework and it makes disk data access so much easier.
My first app BookLibrary, is now on the Mac App Store and uses Disk as it's backend.
With a small adaption of saving NSImages instead, the app uses Disk in these ways:
• Any disk operation of removing, retrieving, and saving.
• Check if app files exist and retrieve data from them.
• Load cached app data often including cover images.
• Save cover preview images to cache via NSImage.
• Saving and retrieving Security Scoped Bookmarks.
Thought you might be interested.
-- mike
I receive the error message when using this line:
Disk.save("testing", to: .temporary, as: "testing")
String is Codable
so it should be supported, right?
Hello, think to your Disk!
When i use the library, the remove method is not friendly to me.
About issue#4,whether there is a similar method to remove a struct to file with data of the same type.
In my handling, I can only remove this file and then save another file. I think it's very bad, hope you can give me a better suggestion or solution.Thinks~
Hello Saoud,
in the readme you wrote to file an issue for question, so I did.
I was wondering if you could help me with a question that I have about your Disk Framework.
I’m trying to save a Dictionary<Int, (UIImage, UIImage)>
Does your framework offer a solution for this or do I need to seperate the dictionary first in order to save? (e.g. create a folder for the int and save the 2 images in it)
Sorry if this question was already answered in your readme, I'm still quite new to Swift/programming in general and could not figure out if the framework can save Dictionaries like that.
Regards,
Michael
d: warning: linking against a dylib which is not safe for use in application extensions: /.../Carthage/Build/iOS/Disk.framework/Disk
Would it be possible to make it safe?
Hey! I was going to save images locally on application support which should be used offline and then I save its url to be able to show them in different places in the application. read on the net that url for applicationSupport can be changed when upgrading the app and i wonder if you take care of it in your code?
Hey there, are there any plans to add expiration dates to the items being stored? In one of my applications, I want to store venue location data to the cache. Ideally I could store this and the data would expire in X time.
Thoughts?
I can't seem to figure out how to get a dictionary of [String: UIImage] from a folder. I would assume this is a basic necessity, as you would need to be able to determine what images you actually saved!
Can someone tell me how to do this if I am missing it, or if it doesn't exist - can this be a feature request!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.