From e44201e0e37e13a2fef58a1547cb8f4f4e0ee616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 25 Feb 2018 09:43:00 +0100 Subject: [PATCH] Apply advice from @sobri909 --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9caefd747..0d2fecb26 100644 --- a/README.md +++ b/README.md @@ -646,7 +646,16 @@ try dbQueue.inDatabase { db in Both arrays and cursors can iterate over database results. How do you choose one or the other? Look at the differences: -- **Cursors can not be used on any thread**: you must consume a cursor on the dispatch queue it was created in. Arrays may be consumed on any thread. +- **Cursors can not be used on any thread**: you must consume a cursor on the dispatch queue it was created in, and you must not extract it. Arrays may be consumed on any thread: + + ```swift + // Wrong + let cursor = try dbQueue.inDatabase { try Player.fetchCursor($0, ...) } + + // OK + let array = try dbQueue.inDatabase { try Player.fetchAll($0, ...) } + ``` + - **Cursors can be iterated only one time.** Arrays can be iterated many times. - **Cursors iterate database results in a lazy fashion**, and don't consume much memory. Arrays contain copies of database values, and may take a lot of memory when there are many fetched results. - **Cursors are granted with direct access to SQLite,** unlike arrays that have to take the time to copy database values. When you really care about performance, you may want to deal with cursors of raw rows at the lowest possible level: `Row.fetchCursor(...)`.