caiyue1993/IceCream

CreamAsset fails save() when given a Specific ID or propName.

開放

#172 建立於 2019年11月4日

 (3 則留言) (0 個反應) (1 位負責人)Swift (259 個分叉)user submission
enhancementgood first issuehelp wanted

倉庫指標

星標
 (2,005 顆星)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

Thank you for the wonderful library.

For example, initializing CreamAsset causes save() inside init() to fail.

CreamAsset.create(object: SomeObject, propName: "https://example.com/", data: someData)

CreamAsset generates uniqueFileName as the concatenation of Object's ID and propName:.

https://github.com/caiyue1993/IceCream/blob/1d6cc98831a743864176e0cc9dbddf8d0ad2e0cc/IceCream/Classes/CreamAsset.swift#L30

Next, CreamAsset.filePath use URL.appendingPathComponent() to generate a filePath from the uniqueFileName generated in the previous section, as shown below.

https://github.com/caiyue1993/IceCream/blob/1d6cc98831a743864176e0cc9dbddf8d0ad2e0cc/IceCream/Classes/CreamAsset.swift#L47

The URL contains the directory separator '/'. Therefore, the generated CreamAsset points to a file in a directory that does not exist in the generated filePath. Therefore, save() called in init() will fail.

https://github.com/caiyue1993/IceCream/blob/1d6cc98831a743864176e0cc9dbddf8d0ad2e0cc/IceCream/Classes/CreamAsset.swift#L31

I can't suggest a solution "correct" because I don't know the set of characters that can't be used as filenames, but as far as I'm concerned, I can work around this problem by making init() generate uniqueFileName like this:.

self.uniqueFileName = "\(objectID)_\(propName)".addingPercentEncoding(withAllowedCharacters: CharacterSet(charactersIn: "/").inverted) ?? "\(objectID)_\(propName)"

Of course, if I don't use "/" for the Object ID or propName, I shouldn't have any problems. However, in my project, URL is used for ID of Object, so I think it is difficult to avoid this problem. If possible, I would appreciate it if IceCream could take care of it. Thank you in advance.

Expected behavior

let asset = CreamAsset.create(object: SomeObject, propName: "https://example.com/", data: someData)

asset.storedData() is not return nil

貢獻者指南