# Synced folders in the Swift SDK (/reference/swift-sdk/folders)

Read and change a synced folder's settings, and make a Mac directory follow it, sync, watch, pause and settle its files.



## Settings [#settings]

`items.folder(_:)` reads a held `Folder` (a `system.folder` item). Its `FolderSettings` hold `title`, `query`, `defaults` for a new file, `include` and `ignore` gitignore patterns (**`ignore` wins**), `firstPlacement`, `outsideQuery` (`.send` or `.hold`), `fullPassIntervalSeconds` (the seconds between a watch's reads of every file, 60 to 86400, `nil` for 600), and two thresholds that make a change wait for confirmation. A `FolderSettingsChange` leaves each `nil` setting as it is, and an interval outside the range throws `invalid` before anything is sent.

| `FolderSettings` field | Default |
| - | - |
| `removalThreshold`: `RemovalThreshold(files:fraction:ceiling:)` | 10 files and 0.25 of the folder's files, and **more files than the `ceiling` of 100 wait whatever their share.** |
| `additionThreshold`: `AdditionThreshold(files:fraction:)` | 50 files and 0.25. |

`copy.createFolder(_:idempotencyKey:)`, `changeFolder(_:_:baseVersion:idempotencyKey:)` and `revokeFolder(_:idempotencyKey:)` write at once, as view writes do. **A revoked folder changes no more.**

## The directory on the Mac [#the-directory-on-the-mac]

`Folders(server:)` works the Mac's directories through the `marfa` command line's registry. **Only `add`, `sync`, `restoreRemoval`, `watch`, and `keepHeld` with `writeBack: true` read the server's sign-in,** and throw `Keychain.Failure` or `SignInError.notSignedIn` when they can't. `pause` and `resume` tell the server when they can read it, and pause or resume without it. Every other call works while the keychain is locked.

| Call | Does |
| - | - |
| `add(_ directory:following:)`, `list()`, `status(of:)` | Make a directory follow a `system.folder`, list the registry, and read a folder's `FolderStatus` with no request. |
| `sync(_:)` | One pass: sends disk changes, catches up, writes out what the query matches, sends placements. Returns a `FolderSyncResult`. |
| `confirmFirstSync(in:)`, `confirmRemoval(in:)`, `restoreRemoval(in:)` | Let a first sync or a held removal go, or cancel a removal and write the files back. |
| `watch(_:)` | Keeps the folder in step while the app runs: a `FolderWatch` sequence of `FolderEvent`s. **Leaving a loop early doesn't stop it:** call `stop()`. |
| `pause(_:)`, `resume(_:)`, `remove(_:)` | Pause or resume a directory, or take the folder off the Mac, leaving plain files. `remove` returns a `FolderRemoval`, or throws `invalid` while writes wait. |
| `removeUnmatched(in:paths:)` | Removes the named [unmatched](/reference/folder-rules#what-a-folder-does-with-each-file) files that are unchanged, leaving their items. Returns `UnmatchedRemoved`: `removed`, `kept` with each `reason`, and `ended`, the placements ended. |
| `keptFiles(in:)`, `discardKept(in:)` | Read the files Marfa set aside, each a `KeptFile` with `path`, `from`, `writers`, `until` and `reason`, or discard the unchanged ones. `discardKept` returns `KeptDiscarded`: `discarded`, `putBack` and `kept`. |

**When another instance answers at the folder's server** ([after a restore](/run/keep-folder-edits-after-a-restore)), `remove(_:)` goes ahead and lists each file it couldn't send in `FolderRemoval.files`. Show every one: a moved or deleted file is in no file. **A first sync waits for the person:** `sync(_:)` returns `.awaitingConfirmation(FirstSyncPlan)` with what it will write and send, and `watch(_:)` throws `firstSyncWaiting`. **A paused directory sends and writes nothing,** and its sync calls throw `syncPaused`. **One process works a folder at a time:** a call asks a watch to let go and waits 10 seconds, then throws `readingHandle`. `status(of:)`, `keptFiles(in:)`, `heldFiles(in:)` and `remembering(_:)` answer beside a watch. **Ask the person before `removeUnmatched` or `discardKept`.** A `StorageCause` on `AddedFolder.storage` warns about iCloud Drive, a cloud service or a network file system: show it once for each folder. `AddedFolder.gitTracked` says Git tracks files there, and `ListedFolder.storeId` is the ID the folder's status on the server reports. A pass reports its `FolderScan` and `FolderPull` counts, and `FolderStatus` gives each file a `FileState` of `.inStep`, `.waiting`, `.held`, `.unmatched`, `.unreached` or `.outside`, a held file's `hold` with its `reason` (`.unsure` for a [held delete](#held-deletes), `.removing` for an interrupted `removeUnmatched` whose file a scan can't tell), and lists in `directories` each directory it flagged, as a `FlaggedFile` with its `path`, `flag`, `reason` and `item`. [Folder sync status](/reference/folder-sync-status) and [How a synced folder treats files](/reference/folder-rules) hold the rules.

## Held deletes [#held-deletes]

When a folder can't tell a deleted file from one moved into another folder, it holds the delete until the person settles it. These calls are the Swift form of `marfa folders held` and `marfa folders forget` ([Settle a held delete](/use/settle-a-held-delete)).

| Call | Does |
| - | - |
| `heldFiles(in:)` | Lists the held files in path order, each a `HeldFile` with `path`, `item`, `missingSince` and `reason`. Empty when none is held. |
| `trashHeld(in:paths:)` | Queues the delete of each named file's item, sent at the next sync. |
| `keepHeld(in:paths:writeBack:)` | Keeps each item and lets the folder's file go. `writeBack: true` writes the file again where the folder's query holds the item. |
| `remembering(_:)` | Previews forgetting a directory that is gone: a `Remembered` naming each `RememberingFolder` that remembers it, with its `held` files that wait on it. Changes nothing. |
| `forget(_:)` | Forgets the directory in each folder the preview named, and returns `Forgotten`. |

**Show the person each file** before settling it. Both settle calls return `SettledHeld`, with each named path in `trashed`, `gone` (already in the bin or gone), `moved` (another folder holds the file, so the item stays), `kept`, `writtenBack`, `notWrittenBack` or `notSettled`. **A file in `notSettled` stays held,** with its reason, and one in `notWrittenBack` is a `FlaggedFile`, flagged `outside_query` when the query no longer holds the item. Neither throws, so check `notSettled` before saying a delete is queued. Both throw `syncPaused` while the sync is paused.

Once the person confirms the directory is gone for good, **pass `forget(_:)` the `Remembered` that `remembering(_:)` returned, unchanged.** A `WaitingHeldFile` whose `stillWaitsOn` is empty is decided again at the next sync, which can move its item to the bin. `Remembered` also holds `listed`, `notRead` (each folder it couldn't read, a `FolderLeft` with its reason) and `alike` (directories that differ only in case, which stay remembered). `Forgotten.forgotten` lists each `ForgottenIn` with `released`, `stillHeld` (each with its `stillWaitsOn`) and `notReleased`, and `notChanged` names each folder left as it was, with why, such as one a watch didn't let go or whose store isn't the one shown.

**Where the registry lists the directory, `forget(_:)` changes nothing and returns `listed` as `true`:** `remove(_:)` it, then preview again. Otherwise `remembering(_:)` throws `invalid` when anything but an empty directory stands at the path. **`forget(_:)` names each folder the preview named in `notChanged` instead,** with what stands there, and changes nothing; it throws `invalid` only when the preview named no folder, read or not. Directories are file `URL`s, and held paths are relative to the folder. [When the other folder is gone](/use/settle-a-held-delete#when-the-other-folder-is-gone) has the rules.

## Directories and tests [#directories-and-tests]

`Client.folderDirectories(_:under:)` in `MarfaAPI` reads [one level of a folder's directories](/reference/api/folders/getFolderDirectories) with their file counts, from the top when `under` is `nil`. Code that takes a `FolderFileManaging` in place of `Folders`, or a `FolderDirectoryReading` in place of `Client`, can be tested with no directory or server: `FakeCopy.folders(removeUnmatched:keptFiles:discardKept:heldFiles:trashHeld:keepHeld:remembering:forget:)` scripts each `FolderFileManaging` call by its name, throwing `unsupported` for one left out, and every value those calls return has a public initializer for the script. `folderClient(directories:)` scripts `folderDirectories` ([Test against a fake copy](/reference/swift-sdk/testing)).
