Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/objective-c-xcode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ jobs:
run: |
xcodebuild build -scheme CoreZen -project CoreZen.xcodeproj

# Builds the documentation from the headers' /// comments and the articles
# in CoreZen/CoreZen.docc. A DocC warning, such as a link to a symbol that
# doesn't exist, fails the step.
- name: Build Documentation
run: |
xcodebuild docbuild -scheme CoreZen -project CoreZen.xcodeproj OTHER_DOCC_FLAGS="--warnings-as-errors"

- name: Test
run: |
xcodebuild test -scheme CoreZen -project CoreZen.xcodeproj \
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ To note a change, add a line under Unreleased, in a group such as Added, Changed

## Unreleased

### Added

- Documentation for the persistence API, built with DocC: doc comments in its headers, which Xcode shows in Quick Help, and a Persistence article with an example.

## 0.1.0 - 2026-11-19

The first tagged version, with the persistence layer, media playback and frame rendering, and utilities that the README describes. Changes made before it are in the git history.
20 changes: 19 additions & 1 deletion CoreZen/Cache/ObjectCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,35 @@

NS_ASSUME_NONNULL_BEGIN

/// Objects by identifier, which any thread can use.
///
/// A repository keeps its objects in one, so that each identifier has one object
/// in memory.
@interface ZENObjectCache : NSObject

/// Makes a cache that holds each object only while something else holds it too.
+ (instancetype)weakObjectCache;

/// Makes a cache that holds each object until it's removed.
+ (instancetype)strongObjectCache;

// nil if nothing with this identifier is cached
/// The object cached with this identifier, or nil if there's none.
- (nullable id)cachedObject:(ZENIdentifier)identifier;

/// Caches an object by its identifier, unless the cache has an object with that
/// identifier already.
///
/// The cache keeps the first object it gets for an identifier, so ignoring a
/// second one keeps the one that the rest of the app may already be using.
- (void)cacheObject:(id<ZENIdentifiable>)object;

/// Removes the object with this identifier, if there's one.
- (void)removeObject:(ZENIdentifier)identifier;

/// Every object in the cache, in no particular order.
- (NSArray<id<ZENIdentifiable>> *)allCachedObjects;

/// Removes every object.
- (void)removeAllObjects;

@end
Expand Down
7 changes: 7 additions & 0 deletions CoreZen/CoreZen.docc/ASSERT_MAIN_THREAD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ``ASSERT_MAIN_THREAD``

Stops the process with an assertion failure unless it's on the main thread.

## Overview

Like any `assert`, it does nothing where `NDEBUG` is defined.
14 changes: 10 additions & 4 deletions CoreZen/CoreZen.docc/CoreZen.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# ``CoreZen``

<!--@START_MENU_TOKEN@-->Summary<!--@END_MENU_TOKEN@-->
Store a domain model in SQLite, play and render media, and more, in a macOS app.

## Overview

<!--@START_MENU_TOKEN@-->Text<!--@END_MENU_TOKEN@-->
CoreZen is an Objective-C framework for macOS 14 or later, on Apple silicon. It has three parts:

- term Persistence: A domain model stored in SQLite, with repositories that load, cache and save its objects asynchronously. It's built on [FMDB](https://github.com/ccgus/fmdb).
- term Media: Playback with [mpv](https://mpv.io), and frames rendered from video files with [FFmpeg](https://ffmpeg.org).
- term Utilities: Smaller AppKit and Foundation helpers, such as a tree of nodes, a preferences window and a work queue.

CoreZen's [README](https://github.com/znelson/CoreZen#readme) says how to get its dependencies and build it.

## Topics

### <!--@START_MENU_TOKEN@-->Group<!--@END_MENU_TOKEN@-->
### Essentials

- <!--@START_MENU_TOKEN@-->``Symbol``<!--@END_MENU_TOKEN@-->
- <doc:Persistence>
157 changes: 157 additions & 0 deletions CoreZen/CoreZen.docc/Persistence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# Persistence

Store a domain model in SQLite, and load, cache and save its objects asynchronously.

## Overview

An app describes each table with three classes:

- A table class, which conforms to ``ZENDatabaseTable``, creates and migrates the table, and reads and writes its rows.
- A data transfer object (DTO), a subclass of ``ZENDataTransferObject``, holds one row's values in properties.
- A domain object, a subclass of ``ZENDomainObject-c.class``, is what the rest of the app works with. It's built around a DTO, and adds whatever the app needs beyond the stored values.

A ``ZENObjectRepository`` brings these together with a ``ZENDatabaseQueue``. It loads the objects, keeps one in memory for each identifier, and saves changes, all asynchronously. Each object has a ``ZENIdentifier`` from ``ZENGetNextObjectIdentifier``, which is unique across every database the process opens.

### Describe a Table

For a table of notes, each with some text:

```objc
@interface NoteDTO : ZENDataTransferObject
@property (nonatomic, copy, nullable) NSString *text;
@end

@interface Note : ZENDomainObject
@end

@interface NoteTable : NSObject <ZENDatabaseTable>
@end

@implementation NoteTable

+ (NSString *)tableName {
return @"notes";
}

+ (BOOL)updateSchema:(ZENDatabase *)database version:(NSUInteger)version {
if (version == 1) {
[database executeUpdate:@"CREATE TABLE notes (identifier INTEGER PRIMARY KEY, text TEXT);"];
return YES;
}
return NO;
}

- (BOOL)insertDTO:(ZENDataTransferObject *)dto database:(ZENDatabase *)database {
NoteDTO *note = (NoteDTO *)dto;
return [database executeUpdate:@"INSERT INTO notes (identifier, text) VALUES (?, ?);",
@(note.identifier), note.text ?: NSNull.null];
}

- (ZENDataTransferObject *)dtoFromRow:(ZENResultSet *)row {
NoteDTO *note = [[NoteDTO alloc] initWithIdentifier:[row longLongIntForColumnIndex:0]];
note.text = [row stringForColumnIndex:1];
return note;
}

- (ZENResultSet *)allRows:(ZENDatabase *)database {
return [database executeQuery:@"SELECT identifier, text FROM notes;"];
}

// updateDTO:database:, deleteByIdentifier:database:, rowByIdentifier:database:
// and countAllRows: work the same way.

@end
```

A later version of the schema adds a branch for version 2, and so on. Each version's change runs once.

### Open the Database

A ``ZENDatabaseSchema`` brings the database up to the latest version of every table's schema:

```objc
ZENDatabaseQueue *queue = [ZENDatabaseQueue databaseQueueWithURL:databaseURL];
ZENDatabaseSchema *schema = [ZENDatabaseSchema schemaWithTableClasses:@[NoteTable.class]];
[queue transactionSync:^(ZENDatabase *database) {
[schema initializeDatabase:database];
}];
```

### Load and Save Objects

A repository for the table makes a `Note` from each row:

```objc
ZENObjectRepository *notes = [[ZENObjectRepository alloc] initWithDataModel:nil
tableClass:NoteTable.class
databaseQueue:queue
domainObjectEmbryo:Note.class
cacheType:ZENObjectRepositoryCacheType_Weak];

NoteDTO *dto = [[NoteDTO alloc] initWithIdentifier:ZENGetNextObjectIdentifier()];
dto.text = @"Buy milk";
[notes addObject:[[Note alloc] initWithDTO:dto] uiCompletion:^(NSError *error) {
// nil once the note is saved, or else why it wasn't
}];

[notes fetchAllObjectsForUI:^(NSArray *fetchedObjects) {
// Every note
}];
```

The repository runs these one after the other on the database queue, so the fetch finds the new note. Each write's completion gets nil once the change is saved, or an error in ``ZENDatabaseErrorDomain`` that says why it wasn't. An object that observes the repository, with ``ZENObjectRepository/zen_addObserver:selector:``, hears about each saved change on the main thread.

## Topics

### Repositories

- ``ZENObjectRepository``

### Domain Objects

- ``ZENDomainObject-c.class``
- ``ZENDomainObject-c.protocol``
- ``ZENDataModel``

### Tables

- ``ZENDatabaseTable``
- ``ZENDataTransferObject``
- ``ZENDatabaseSchema``

### Databases

- ``ZENDatabaseQueue``
- ``ZENDatabase``
- ``ZENResultSet``
- ``ZENDatabaseErrorDomain``
- ``ZENDatabaseErrorCode``

### Identifiers

- ``ZENIdentifier``
- ``ZENInvalidIdentifier``
- ``ZENIdentifiable``
- ``ZENGetNextObjectIdentifier``
- ``ZENSetLargestObjectIdentifier``

### Caching

- ``ZENObjectCache``

### Asynchronous Callbacks

- ``ZENAsyncCompletionBlock``
- ``ZENAsyncContinueBlock``
- ``ZENFetchResultsBlock``
- ``ZENAsyncCountCompletionBlock``
- ``ZENCallAsyncCompletionBlockOnThreadPool``
- ``ZENCallAsyncCompletionBlockOnMainThread``
- ``ZENCallAsyncContinueBlockOnThreadPool``
- ``ZENCallAsyncContinueBlockOnMainThread``
- ``ZENCallFetchResultsBlockOnThreadPool``
- ``ZENCallFetchResultsBlockOnMainThread``
- ``ZENCallAsyncCountCompletionBlockOnThreadPool``
- ``ZENCallAsyncCountCompletionBlockOnMainThread``
- ``ZENDeliverNotificationOnMainThread``
- ``ASSERT_MAIN_THREAD``
29 changes: 29 additions & 0 deletions CoreZen/CoreZen.docc/ZENDatabaseQueue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# ``ZENDatabaseQueue``

## Topics

### Creating a Queue

- ``databaseQueueWithURL:``
- ``databaseQueueWithURL:options:``
- ``databaseQueueInMemory``
- ``databaseQueueInMemoryWithOptions:``
- ``ZENDatabaseQueueOptions``

### Running Blocks

- ``transactionAsync:``
- ``transactionSync:``
- ``fetchAsync:``
- ``fetchSync:``
- ``ZENDatabaseBlock``

### Closing the Database

- ``shutdown``
- ``shutdown:``

### Maintenance and Debugging

- ``vacuumAsync``
- ``traceExecution``
51 changes: 51 additions & 0 deletions CoreZen/CoreZen.docc/ZENObjectRepository.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# ``ZENObjectRepository``

## Topics

### Creating a Repository

- ``initWithDataModel:tableClass:databaseQueue:domainObjectEmbryo:cacheType:``
- ``ZENObjectRepositoryCacheType``

### Fetching Objects

- ``fetchAllObjects:``
- ``fetchAllObjectsForUI:``
- ``fetchObjectByIdentifier:completion:``
- ``fetchObjectByIdentifier:uiCompletion:``
- ``fetchObjectsWithQuery:completion:``
- ``fetchObjectsWithQuery:uiCompletion:``
- ``ZENRepositoryQuery``

### Counting and Summing

- ``countAllObjects:``
- ``countAllObjectsForUI:``
- ``sumColumn:completion:``
- ``sumColumn:uiCompletion:``

### Saving Objects

- ``addObject:completion:``
- ``addObject:uiCompletion:``
- ``asyncInitAndAdd:completion:``
- ``asyncInitAndAdd:uiCompletion:``
- ``updateObject:completion:``
- ``updateObject:uiCompletion:``
- ``deleteObject:completion:``
- ``deleteObject:uiCompletion:``

### Saving Several Writes Together

- ``performWrites:completion:``
- ``performWrites:uiCompletion:``
- ``ZENRepositoryWriteBatch``

### Observing Changes

- ``zen_addObserver:selector:``
- ``zen_removeObserver:``
- ``ZENObjectRepositoryObjectAddedNotification``
- ``ZENObjectRepositoryObjectUpdatedNotification``
- ``ZENObjectRepositoryObjectDeletedNotification``
- ``ZENObjectRepositoryNotificationObjectKey``
30 changes: 30 additions & 0 deletions CoreZen/CoreZen.docc/ZENResultSet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ``ZENResultSet``

## Topics

### Moving Through Rows

- ``next``
- ``close``

### Reading by Column Index

- ``intForColumnIndex:``
- ``longLongIntForColumnIndex:``
- ``unsignedLongLongIntForColumnIndex:``
- ``doubleForColumnIndex:``
- ``boolForColumnIndex:``
- ``stringForColumnIndex:``
- ``dataForColumnIndex:``
- ``dateForColumnIndex:``

### Reading by Column Name

- ``intForColumn:``
- ``longLongIntForColumn:``
- ``unsignedLongLongIntForColumn:``
- ``doubleForColumn:``
- ``boolForColumn:``
- ``stringForColumn:``
- ``dataForColumn:``
- ``dateForColumn:``
12 changes: 8 additions & 4 deletions CoreZen/CoreZen.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@

#import <Foundation/Foundation.h>

// CoreZen's version, such as 0.1.0, is its bundle's CFBundleShortVersionString.
// These two come from Xcode's build number for the framework instead, which
// stays 1 (CURRENT_PROJECT_VERSION).

/// Xcode's build number for the framework, which stays 1.
///
/// CoreZen's version, such as 0.1.0, is its bundle's
/// `CFBundleShortVersionString`, not this.
FOUNDATION_EXPORT double CoreZenVersionNumber;

/// Xcode's version string for the framework, made from its build number.
///
/// CoreZen's version, such as 0.1.0, is its bundle's
/// `CFBundleShortVersionString`, not this.
FOUNDATION_EXPORT const unsigned char CoreZenVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <CoreZen/PublicHeader.h>
Expand Down
Loading