Skip to content

Commit

Permalink
Don't return a nil asset if the file already exists at the copied loc…
Browse files Browse the repository at this point in the history
…ation. Removed the unused/unsaved data field. Removed redundant code.
  • Loading branch information
Patrick Roy authored and caiyue1993 committed Aug 4, 2020
1 parent 14ad5c1 commit 2bae5d2
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions IceCream/Classes/CreamAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ import CloudKit
/// So this is the deal.
public class CreamAsset: Object {
@objc dynamic private var uniqueFileName = ""
@objc dynamic private var data: Data?
override public static func ignoredProperties() -> [String] {
return ["data", "filePath"]
return ["filePath"]
}

private convenience init(objectID: String, propName: String, data: Data? = nil) {
private convenience init(objectID: String, propName: String) {
self.init()
self.data = data
self.uniqueFileName = "\(objectID)_\(propName)"
}

Expand Down Expand Up @@ -80,11 +78,9 @@ public class CreamAsset: Object {
/// - shouldOverwrite: Whether to try and save the file even if an existing file exists for the same object.
/// - Returns: A CreamAsset if it was successful
public static func create(object: CKRecordConvertible, propName: String, data: Data, shouldOverwrite: Bool = true) -> CreamAsset? {
let creamAsset = CreamAsset(objectID: object.recordID.recordName,
propName: propName,
data: data)
save(data: data, to: creamAsset.uniqueFileName, shouldOverwrite: shouldOverwrite)
return creamAsset
return create(objectID: object.recordID.recordName,
propName: propName,
data: data)
}

/// Creates a new CreamAsset for the given object id with Data
Expand All @@ -97,8 +93,7 @@ public class CreamAsset: Object {
/// - Returns: A CreamAsset if it was successful
public static func create(objectID: String, propName: String, data: Data, shouldOverwrite: Bool = true) -> CreamAsset? {
let creamAsset = CreamAsset(objectID: objectID,
propName: propName,
data: data)
propName: propName)
save(data: data, to: creamAsset.uniqueFileName, shouldOverwrite: shouldOverwrite)
return creamAsset
}
Expand All @@ -111,14 +106,9 @@ public class CreamAsset: Object {
/// - url: The URL where the file located
/// - Returns: A CreamAsset if it was successful
public static func create(object: CKRecordConvertible, propName: String, url: URL) -> CreamAsset? {
let creamAsset = CreamAsset(objectID: object.recordID.recordName, propName: propName)
do {
try FileManager.default.copyItem(at: url, to: creamAsset.filePath)
} catch {
/// Log: copy item failed
return nil
}
return creamAsset
return create(objectID: object.recordID.recordName,
propName: propName,
url: url)
}


Expand All @@ -130,11 +120,13 @@ public class CreamAsset: Object {
/// - Returns: The CreamAsset if creates successful
public static func create(objectID: String, propName: String, url: URL) -> CreamAsset? {
let creamAsset = CreamAsset(objectID: objectID, propName: propName)
do {
try FileManager.default.copyItem(at: url, to: creamAsset.filePath)
} catch {
/// Log: copy item failed
return nil
if !FileManager.default.fileExists(atPath: creamAsset.filePath.path) {
do {
try FileManager.default.copyItem(at: url, to: creamAsset.filePath)
} catch {
/// Log: copy item failed
return nil
}
}
return creamAsset
}
Expand Down

0 comments on commit 2bae5d2

Please sign in to comment.