Skip to content

Commit 73de364

Browse files
fatbobmanclaude
andcommitted
Add CloudKit usage recommendations and Apple's 7-day cleanup strategy
- Add recommended 7-day cleanup strategy section based on Apple's documentation - Add important notes for NSPersistentCloudKitContainer users - Explain potential NSPersistentHistoryTokenExpiredError issues with aggressive cleanup - Recommend using .byDuration(seconds: 60 * 60 * 24 * 7) for CloudKit scenarios - Clarify that includingCloudKitMirroring should NOT be set to true in CloudKit scenarios Related to issue #7 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 9436f73 commit 73de364

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,38 @@ cleanStrategy: .byNotification(times: 1),
179179
cleanStrategy: .byDuration(seconds: 60),
180180
```
181181
182+
#### Recommended Strategy
183+
184+
According to Apple's documentation, it's recommended to use a 7-day cleanup strategy to achieve a good balance between performance and storage capacity:
185+
186+
```swift
187+
cleanStrategy: .byDuration(seconds: 60 * 60 * 24 * 7), // 7 days
188+
```
189+
190+
This strategy allows sufficient time for all authors (including app extensions and CloudKit) to process and merge transactions before cleanup occurs.
191+
192+
#### Important: Using with NSPersistentCloudKitContainer
193+
194+
**Note:** The default cleanup strategy `.byNotification(times: 1)` can be too aggressive when using CloudKit synchronization and may cause `NSPersistentHistoryTokenExpiredError` (error code 134301), potentially leading to local database purges and re-syncing from CloudKit.
195+
196+
When using CloudKit synchronization, CloudKit internally relies on persistent history to track synchronization state. If history is cleaned up too frequently, CloudKit may lose its tracking tokens before completing its internal operations.
197+
198+
**Recommended strategy:**
199+
200+
Use a time-based cleanup strategy with sufficient duration (such as 7 days) to give CloudKit enough time to process the persistent history:
201+
202+
```swift
203+
let kit = PersistentHistoryTrackingKit(
204+
container: container,
205+
currentAuthor: "app1",
206+
allAuthors: ["app1", "app2"],
207+
cleanStrategy: .byDuration(seconds: 60 * 60 * 24 * 7), // 7 days
208+
userDefaults: userDefaults
209+
)
210+
```
211+
212+
**Important:** Do NOT set `includingCloudKitMirroring: true` in this scenario, as CloudKit handles its own synchronization internally. Setting it to true would incorrectly merge CloudKit's internal transactions into your app's contexts. Instead, use a longer cleanup interval to ensure CloudKit has sufficient time to use the persistent history before it gets cleaned up.
213+
182214
When the cleanup policy is set to none, cleanup can be performed at the right time by generating separate cleanup instances.
183215
184216
```swift

READMECN.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,38 @@ cleanStrategy: .byNotification(times: 1),
179179
cleanStrategy: .byDuration(seconds: 60),
180180
```
181181
182+
#### 推荐策略
183+
184+
根据 Apple 的文档建议,推荐使用 7 天的清理策略,以在性能和存储容量之间取得良好的平衡:
185+
186+
```swift
187+
cleanStrategy: .byDuration(seconds: 60 * 60 * 24 * 7), // 7 天
188+
```
189+
190+
这种策略允许所有 author(包括应用扩展和 CloudKit)有足够的时间来处理和合并事务,然后再进行清理
191+
192+
#### 重要:与 NSPersistentCloudKitContainer 配合使用
193+
194+
**注意:** 默认的清理策略 `.byNotification(times: 1)` 在使用 CloudKit 同步时可能过于激进,可能导致 `NSPersistentHistoryTokenExpiredError`(错误代码 134301),从而导致本地数据库被清空并重新从 CloudKit 同步
195+
196+
当使用 CloudKit 同步时,CloudKit 内部依赖 persistent history 来跟踪同步状态如果历史记录清理过于频繁,CloudKit 可能会在完成其内部操作之前丢失其跟踪令牌
197+
198+
**推荐策略:**
199+
200+
使用基于时间的清理策略,并设置足够的持续时间(如 7 天),以给 CloudKit 足够的时间来处理 persistent history:
201+
202+
```swift
203+
let kit = PersistentHistoryTrackingKit(
204+
container: container,
205+
currentAuthor: "app1",
206+
allAuthors: ["app1", "app2"],
207+
cleanStrategy: .byDuration(seconds: 60 * 60 * 24 * 7), // 7 天
208+
userDefaults: userDefaults
209+
)
210+
```
211+
212+
**重要提示:** 在此场景下,请勿设置 `includingCloudKitMirroring: true`,因为 CloudKit 会在内部处理自己的同步将其设置为 true 会错误地将 CloudKit 的内部事务合并到您的应用上下文中相反,应使用更长的清理间隔,以确保 CloudKit 在清理之前有足够的时间使用 persistent history
213+
182214
当清理策略设置为 none 时,可以通过生成单独的清理实例,在合适的时机进行清理
183215
184216
```swift

0 commit comments

Comments
 (0)