ios - compile error with rows.next() in GRDB and Swift 3 -
i developing ios app using swift 3 , grdb sqlite library. following code fragment causes compile error of "value of type '[row]' has no member 'next'"
let rows = try row.fetchall(db, sql, arguments:arguments) while let row = try rows.next() { <--- line error ... }
as far can tell following example in docs correctly. can tell me doing wrong?
the fetchall
method returns regular swift array, iterate other arrays:
let rows = try row.fetchall(db, sql, arguments:arguments) // [row] row in rows { // use row }
the next
method belongs cursors, fetchcursor
method. cursors not arrays because don't load database results in 1 step. instead, cursors iterated row after row:
let rows = try row.fetchcursor(db, sql, arguments:arguments) // databasecursor<row> while let row = try rows.next() { // use row }
you see both arrays , cursors can iterate database results. how choose 1 or other? @ differences:
- arrays contain copies of database values , may consumed on thread.
- arrays can can take lot of memory, if number of fetched results high.
- arrays can iterated many times.
- cursors iterate on database results in lazy fashion, , don't consume memory.
- cursors faster because go straight sqlite , don't copy database values unless necessary.
- cursors can not used on thread.
- cursors can iterated 1 time.
compare:
// on main thread: let (rowarray, rowcursor) = try dbqueue.indatabase { db -> ([row], databasecursor<row>) in let rowarray = try row.fetchall(db, "select ...") let rowcursor = try row.fetchcursor(db, "select ...") // ok row in rowarray { ... } while let row = try rowcursor.next() { ... } // second iteration row in rowarray { ... } // same rows while let row = try rowcursor.next() { ... } // no result return (rowarray, rowcursor) } // ok: arrays can consumed on thread row in rowarray { ... } dispatchqueue.global(.default).async { row in rowarray { ... } } // don't that, never while let row = try rowcursor.next() { ... } dispatchqueue.global(.default).async { while let row = try rowcursor.next() { ... } }
if don't see, or don't care difference, use arrays. if care memory , performance, use cursors when appropriate.
link documentation: https://github.com/groue/grdb.swift/blob/master/readme.md#fetching-methods
Comments
Post a Comment