{"id":6915,"date":"2025-11-02T10:11:33","date_gmt":"2025-11-02T02:11:33","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6915"},"modified":"2025-11-02T10:11:33","modified_gmt":"2025-11-02T02:11:33","slug":"cloudkit-cloud-sandbox-%e6%96%87%e4%bb%b6%e5%90%88%e5%b9%b6%e6%98%be%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6915","title":{"rendered":"CloudKit Cloud\/Sandbox \u6587\u4ef6\u5408\u5e76\u663e\u793a"},"content":{"rendered":"<p>\u975e\u5e38\u597d\u7684\u95ee\u9898 \ud83d\udc4d\uff0c\u8fd9\u5176\u5b9e\u662f <strong>\u6587\u4ef6\u7ba1\u7406\u7c7b App \u4f7f\u7528 CloudKit\/iCloud \u65f6\u7684\u6838\u5fc3\u8bbe\u8ba1\u96be\u70b9\u4e4b\u4e00<\/strong>\u3002\u4f60\u7684\u76ee\u6807\u662f\u2014\u2014<strong>\u5927\u6587\u4ef6\u4ec5\u5b58\u5728\u672c\u5730\u6c99\u76d2\uff0c\u5c0f\u6587\u4ef6\u540c\u6b65\u5230 iCloud\uff0c\u4f46\u4e24\u8005\u5728 UI \u4e0a\u8981\u7edf\u4e00\u5c55\u793a\u3001\u5e76\u4e14\u64cd\u4f5c\u4f53\u9a8c\u4e00\u81f4<\/strong>\u3002\u4e0b\u9762\u662f\u7cfb\u7edf\u6027\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\u8981\u70b9\uff1a<\/p>\n<hr \/>\n<h2>\ud83e\udde9 \u76ee\u6807<\/h2>\n<blockquote>\n<p>\u7528\u6237\u5728\u6587\u4ef6\u7ba1\u7406\u5e94\u7528\u4e2d\u770b\u5230\u7684\u76ee\u5f55\u7ed3\u6784\uff0c\u662f<strong>\u672c\u5730\u6c99\u76d2\u76ee\u5f55 + iCloud \u76ee\u5f55\u7684\u5408\u5e76\u89c6\u56fe<\/strong>\uff0c<br \/>\n\u540c\u65f6\u8981\u89e3\u51b3\uff1a<\/p>\n<ul>\n<li>\u4e24\u8fb9\u8def\u5f84\u4e00\u81f4\u7684\u6587\u4ef6\u5939\u8981\u5408\u5e76\u5c55\u793a\uff1b<\/li>\n<li>\u6587\u4ef6\u662f\u5426\u5728 iCloud \u4e2d\u8981\u6709\u6e05\u6670\u72b6\u6001\uff1b<\/li>\n<li>\u907f\u514d\u5927\u6587\u4ef6\u4e0a\u4f20\uff1b<\/li>\n<li>\u6587\u4ef6\u64cd\u4f5c\uff08\u590d\u5236\u3001\u5220\u9664\u3001\u79fb\u52a8\uff09\u4fdd\u6301\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<hr \/>\n<h2>\u4e00\u3001\u603b\u4f53\u8bbe\u8ba1\u601d\u8def<\/h2>\n<h3>\u2705 1. \u53cc\u76ee\u5f55\u7edf\u4e00\u89c6\u56fe (Merged View Model)<\/h3>\n<p>\u6784\u5efa\u4e00\u4e2a\u903b\u8f91\u4e0a\u7684\u6587\u4ef6\u6811\u6a21\u578b\uff08ViewModel\uff09\uff0c\u5176\u6570\u636e\u6e90\u7531\u4e24\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6765\u6e90<\/th>\n<th>\u8def\u5f84\u6839<\/th>\n<th>\u5b58\u50a8\u4f4d\u7f6e<\/th>\n<th>\u662f\u5426\u53c2\u4e0e iCloud \u540c\u6b65<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u672c\u5730<\/td>\n<td><code>&lt;App Sandbox&gt;\/Documents\/<\/code><\/td>\n<td>\u8bbe\u5907\u672c\u5730<\/td>\n<td>\u2705\uff08\u53ef\u9009\uff09<\/td>\n<\/tr>\n<tr>\n<td>iCloud<\/td>\n<td><code>&lt;iCloudContainer&gt;\/Documents\/<\/code><\/td>\n<td>\u4e91\u7aef<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f60\u7684\u89c6\u56fe\u5c42\uff08\u5982 FileListView\uff09\u5c55\u793a\u7684\u76ee\u5f55\u662f\u4e24\u8005\u5408\u5e76\u540e\u7684\u7ed3\u679c\u3002<br \/>\n<strong>\u76f8\u5bf9\u8def\u5f84\u76f8\u540c\u7684\u76ee\u5f55\u8981\u5408\u5e76\u4e3a\u4e00\u4e2a\u8282\u70b9\u3002<\/strong><\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code>\u672c\u5730:   \/Documents\/Work\/report.docx\niCloud: \/Documents\/Work\/summary.pdf<\/code><\/pre>\n<p>\u2192 \u5408\u5e76\u5c55\u793a\u4e3a\uff1a<\/p>\n<pre><code>\/Work\/\n   \u251c\u2500\u2500 report.docx (local only)\n   \u2514\u2500\u2500 summary.pdf (iCloud)<\/code><\/pre>\n<hr \/>\n<h3>\u2705 2. \u6587\u4ef6\u6807\u8bc6\u4e0e\u5143\u6570\u636e\uff08FileMetadata\uff09<\/h3>\n<p>\u6bcf\u4e2a\u6587\u4ef6\u9700\u8981\u4e00\u4e2a\u7edf\u4e00\u7684\u5143\u6570\u636e\u6a21\u578b\uff1a<\/p>\n<pre><code class=\"language-swift\">struct FileMetadata {\n    let name: String\n    let relativePath: String\n    let isDirectory: Bool\n    let isInICloud: Bool\n    let isDownloaded: Bool\n    let fileSize: Int64\n    let lastModified: Date\n    let localURL: URL?\n    let cloudURL: URL?\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6a21\u578b\u7528\u4e8e\u524d\u7aef\u5c55\u793a\uff08\u5982\u5217\u8868 icon\u3001\u540c\u6b65\u72b6\u6001\uff09\u4ee5\u53ca\u540c\u6b65\u903b\u8f91\u5224\u65ad\u3002<\/p>\n<hr \/>\n<h2>\u4e8c\u3001\u5927\u5c0f\u6587\u4ef6\u7b56\u7565<\/h2>\n<h3>\ud83e\udeb6 \u5c0f\u6587\u4ef6\uff08&lt; \u9608\u503c\uff0c\u5982 20MB\uff09<\/h3>\n<ul>\n<li>\u81ea\u52a8\u540c\u6b65\u5230 iCloud\uff08\u5373\u5b58\u50a8\u5728 iCloud \u5bb9\u5668\u76ee\u5f55\u4e2d\uff09<\/li>\n<li>\u5f53\u7528\u6237\u5728\u8bbe\u5907\u4e0a\u7f16\u8f91\u65f6\u81ea\u52a8\u89e6\u53d1 <code>NSFileCoordinator<\/code> \u540c\u6b65\u3002<\/li>\n<\/ul>\n<h3>\ud83e\udea8 \u5927\u6587\u4ef6\uff08\u2265 \u9608\u503c\uff09<\/h3>\n<ul>\n<li><strong>\u4ec5\u5b58\u50a8\u5728\u672c\u5730\u6c99\u76d2\u76ee\u5f55<\/strong>\uff1b<\/li>\n<li>\u5728\u5408\u5e76\u5c55\u793a\u65f6\u540c\u6837\u51fa\u73b0\u5728\u76f8\u5e94\u7684\u8def\u5f84\u4f4d\u7f6e\uff1b<\/li>\n<li>\u4f46\u52a0\u4e0a\u6807\u8bc6 \u201c\u672c\u5730\u6587\u4ef6\u201d\uff1b<\/li>\n<li>\u5982\u679c\u7528\u6237\u5f00\u542f\u201c\u4e0a\u4f20\u201d\u64cd\u4f5c\uff0c\u53ef\u4ee5\u9009\u62e9\u624b\u52a8\u8fc1\u79fb\u5230 iCloud\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e09\u3001\u5c55\u793a\u903b\u8f91<\/h2>\n<h3>\ud83d\udcc1 \u6587\u4ef6\u5408\u5e76\u7b97\u6cd5\u793a\u610f<\/h3>\n<pre><code class=\"language-swift\">func mergeFileLists(local: [FileMetadata], cloud: [FileMetadata]) -&gt; [FileMetadata] {\n    var merged: [FileMetadata] = []\n    let allKeys = Set(local.map(\\.relativePath)).union(cloud.map(\\.relativePath))\n\n    for key in allKeys {\n        let localFile = local.first { $0.relativePath == key }\n        let cloudFile = cloud.first { $0.relativePath == key }\n\n        let mergedFile = FileMetadata(\n            name: localFile?.name ?? cloudFile?.name ?? &quot;&quot;,\n            relativePath: key,\n            isDirectory: localFile?.isDirectory ?? cloudFile?.isDirectory ?? false,\n            isInICloud: cloudFile != nil,\n            isDownloaded: localFile != nil,\n            fileSize: localFile?.fileSize ?? cloudFile?.fileSize ?? 0,\n            lastModified: max(localFile?.lastModified ?? .distantPast,\n                              cloudFile?.lastModified ?? .distantPast),\n            localURL: localFile?.localURL,\n            cloudURL: cloudFile?.cloudURL\n        )\n        merged.append(mergedFile)\n    }\n\n    return merged.sorted { $0.name.lowercased() &lt; $1.name.lowercased() }\n}<\/code><\/pre>\n<hr \/>\n<h2>\u56db\u3001\u76ee\u5f55\u76d1\u542c\u4e0e\u66f4\u65b0<\/h2>\n<p>\u4e3a\u4e86\u4fdd\u6301\u5c55\u793a\u5b9e\u65f6\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>NSMetadataQuery<\/code> \u76d1\u542c iCloud \u5bb9\u5668\uff1b<\/li>\n<li>\u4f7f\u7528 <code>FileManager<\/code> + <code>DispatchSourceFileSystemObject<\/code> \u76d1\u542c\u672c\u5730\u6c99\u76d2\u76ee\u5f55\uff1b<\/li>\n<li>\u5f53\u4e24\u8fb9\u76ee\u5f55\u6709\u53d8\u5316\u65f6\uff0c\u91cd\u65b0\u6267\u884c merge\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e94\u3001\u6587\u4ef6\u64cd\u4f5c\u7edf\u4e00\u5c01\u88c5<\/h2>\n<p>\u5b9e\u73b0\u4e00\u4e2a\u4e2d\u95f4\u5c42 <code>FileAccessManager<\/code>\uff0c\u6240\u6709\u64cd\u4f5c\u8d70\u7edf\u4e00\u63a5\u53e3\uff1a<\/p>\n<pre><code class=\"language-swift\">func delete(_ file: FileMetadata)\nfunc move(_ file: FileMetadata, to destination: URL)\nfunc copy(_ file: FileMetadata, to destination: URL)\nfunc uploadToICloud(_ file: FileMetadata)\nfunc downloadFromICloud(_ file: FileMetadata)<\/code><\/pre>\n<ul>\n<li>\u5185\u90e8\u5224\u65ad\u6587\u4ef6\u6765\u6e90\uff08local \/ iCloud\uff09\uff0c\u6267\u884c\u5bf9\u5e94\u903b\u8f91\uff1b<\/li>\n<li>\u5bf9 UI \u5c42\u800c\u8a00\uff0c\u6240\u6709\u64cd\u4f5c\u662f\u4e00\u81f4\u7684\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2>\u516d\u3001\u72b6\u6001\u63d0\u793a\u5efa\u8bae\uff08UI\uff09<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u72b6\u6001<\/th>\n<th>\u56fe\u6807\/\u6807\u8bc6<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u672c\u5730\u672a\u540c\u6b65<\/td>\n<td>\ud83d\udcc1 Local<\/td>\n<\/tr>\n<tr>\n<td>\u5df2\u540c\u6b65 iCloud<\/td>\n<td>\u2601\ufe0f Synced<\/td>\n<\/tr>\n<tr>\n<td>iCloud \u672a\u4e0b\u8f7d<\/td>\n<td>\u2601\ufe0f\u2b07\ufe0f<\/td>\n<\/tr>\n<tr>\n<td>\u4e0a\u4f20\u4e2d<\/td>\n<td>\ud83d\udd04<\/td>\n<\/tr>\n<tr>\n<td>\u4e0b\u8f7d\u4e2d<\/td>\n<td>\u2b07\ufe0f Progress<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>\u4e03\u3001\u53ef\u6269\u5c55\u7b56\u7565\uff08\u8fdb\u9636\uff09<\/h2>\n<ul>\n<li>\n<p>\u652f\u6301\u7528\u6237\u914d\u7f6e\u201c\u540c\u6b65\u7b56\u7565\u201d\uff1a<\/p>\n<ul>\n<li>\u81ea\u52a8\u540c\u6b65\uff08\u9ed8\u8ba4\u4ec5\u5c0f\u6587\u4ef6\uff09<\/li>\n<li>\u624b\u52a8\u540c\u6b65<\/li>\n<li>\u4ec5 Wi-Fi \u540c\u6b65<\/li>\n<\/ul>\n<\/li>\n<li>\u7ed3\u5408 <code>CKAsset<\/code> \u4e0e CloudKit Record \u5b9e\u73b0\u6587\u4ef6\u5143\u6570\u636e\u540c\u6b65\uff08\u53ef\u8de8\u8bbe\u5907\u6062\u590d\u76ee\u5f55\u7ed3\u6784\uff09\uff1b<\/li>\n<li>\u5bf9\u5927\u6587\u4ef6\uff0c\u53ef\u63d0\u4f9b\u201c\u4e0a\u4f20\u5360\u4f4d\u7b26\u201d\u673a\u5236\uff0c\u8bb0\u5f55\u6587\u4ef6\u8def\u5f84\u4f46\u4e0d\u771f\u6b63\u4e0a\u4f20\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2>\u2705 \u603b\u7ed3<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u6a21\u5757<\/th>\n<th>\u5173\u952e\u8981\u70b9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u6587\u4ef6\u5c55\u793a<\/td>\n<td>\u6c99\u76d2 + iCloud \u5408\u5e76\u89c6\u56fe<\/td>\n<\/tr>\n<tr>\n<td>\u540c\u6b65\u7b56\u7565<\/td>\n<td>\u5c0f\u6587\u4ef6\u81ea\u52a8\u540c\u6b65\uff0c\u5927\u6587\u4ef6\u672c\u5730\u4fdd\u5b58<\/td>\n<\/tr>\n<tr>\n<td>\u5143\u6570\u636e\u7ed3\u6784<\/td>\n<td>\u7edf\u4e00\u6a21\u578b\uff0c\u652f\u6301\u6765\u6e90\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td>\u6587\u4ef6\u76d1\u542c<\/td>\n<td>\u53cc\u5411\u76d1\u542c\u5e76\u81ea\u52a8 merge<\/td>\n<\/tr>\n<tr>\n<td>\u6587\u4ef6\u64cd\u4f5c<\/td>\n<td>\u62bd\u8c61\u7edf\u4e00\u63a5\u53e3\u5c42<\/td>\n<\/tr>\n<tr>\n<td>UI<\/td>\n<td>\u72b6\u6001\u660e\u786e\u3001\u5b9e\u65f6\u66f4\u65b0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<p>\u5982\u679c\u4f60\u613f\u610f\uff0c\u6211\u53ef\u4ee5\u5e2e\u4f60\u753b\u4e00\u5f20\u7ed3\u6784\u56fe\uff0c\u5c55\u793a\uff1a<\/p>\n<ul>\n<li>\u6c99\u76d2\u76ee\u5f55<\/li>\n<li>iCloud \u76ee\u5f55<\/li>\n<li>Merged ViewModel \u7684\u6570\u636e\u6d41<\/li>\n<li>\u6587\u4ef6\u64cd\u4f5c\u6d41\uff08\u4e0a\u4f20\u3001\u4e0b\u8f7d\u3001\u5408\u5e76\uff09<\/li>\n<\/ul>\n<p>\u662f\u5426\u8981\u6211\u5e2e\u4f60\u753b\u8fd9\u5f20\u67b6\u6784\u56fe\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u5e38\u597d\u7684\u95ee\u9898 \ud83d\udc4d\uff0c\u8fd9\u5176\u5b9e\u662f \u6587\u4ef6\u7ba1\u7406\u7c7b App \u4f7f\u7528 CloudKit\/iCloud \u65f6\u7684\u6838\u5fc3\u8bbe\u8ba1\u96be\u70b9\u4e4b\u4e00\u3002\u4f60&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6915\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-6915","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6915"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6915\/revisions"}],"predecessor-version":[{"id":6916,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6915\/revisions\/6916"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}