1

I synchronize iCloud Drive and local storage (Documents folder) in my app. But there is 1 bug.

1) device 1(or 2) -> copy files from documents to iCloud container

2) device 1(or 2) -> read files from iCloud container to documents - all good.

But:

1) device 1 -> copy files from documents to iCloud container

2) device 2 -> copy files from iCloud container to Documents - and here bug. All files normally was copyed, but it's broken and I can't open it in Documents (but can see it).

On the screen below 1 and 3 files was created on current device, and 2, 4 and 5 files was created on other device and was synchronized with iCloud container. And more: normally file size (1 and 3) - 32kb, broken files - 2kb.

enter image description here

enter image description here

My code (I use this function for copy from documents to iCloud and for copy from iCloud to documents):

    private static func copyFilesFrom(url: URL, toURL: URL, folder: String) {

    let fileManager = FileManager.default

    do {

        let newDir = toURL.appendingPathComponent(folder)
        if !fileManager.fileExists(atPath: newDir.absoluteString) {

            try fileManager.createDirectory(at: newDir,
                                            withIntermediateDirectories: true,
                                            attributes: nil)

        } else {
            print("Already created catalog with folder: \(folder)")
        }

        let dirs = try fileManager.contentsOfDirectory(atPath: url.appendingPathComponent(folder).path)

        for dir in dirs {
            do {
                try fileManager.copyItem(at: url.appendingPathComponent(folder).appendingPathComponent(dir),
                                         to: newDir.appendingPathComponent(dir))
            } catch {
                print(error)
            }
        }
    } catch {
        print(error)
    }
}

0 Answers0