Comments (5)
Hello @robert-cronin,
Your sample code looks like you want to drive the content of the database from a textfield. But is it really the case? Don't you want instead to have the content of the database drive the UI?
If this is the case, then the code below, inspired from RxSwift sample code, should work as expected:
// Number of players
let allPlayersRequest = Player.all()
allPlayersRequest.rx
.fetchCount(in: dbQueue)
.map { "\($0) players" }
.bind(to: playerCountLabel.rx.text)
// Maximum score
let maximumScoreRequest = Player
.select(max(Column("score")))
.asRequestOf(Int.self)
maximumScoreRequest.rx
.fetchOne(in: dbQueue)
.map { "\($0 ?? 0)" }
.bind(to: maximumScoreLabel.rx.text)
// Name of player 1
Player.filter(key: 1).rx
.fetchOne(in: dbQueue)
.map { $0?.name ?? "" }
.bind(to: playerNameLabel.rx.text)
Now of course, you may sometimes need to update several UI elements on each database change. For example, if you track a single record, you may write something like:
Player.filter(key: 1).rx
.fetchOne(in: dbQueue)
.subscribe(onNext: { player in
if player = player {
// Update UI for updated Player 1
} else {
// Update UI for deleted Player 1
}
})
from rxgrdb.
Oh, and I have to give you an extra advice when I read:
FLIGHTS.filter(sql: "FLIGHT_ID = '\(currentFlightID)'")
You should really stop embedding raw values in SQL, and start using arguments instead:
FLIGHTS.filter(sql: "FLIGHT_ID = ?", arguments: [currentFlightID})
Another valid option:
FLIGHTS.filter(Column("FLIGHT_ID") == currentFlightID)
Sorry if I sound like a pontificating teacher, but this has to become a reflex: NEVER EMBED RAW VALUES IN YOUR SQL See Avoiding SQL Injection for more information.
from rxgrdb.
Thanks groue!
I have tried to implement a simple binding but keep coming up with the following error:
Fatal error: could not convert database value NULL to String
I have a flights table with an aircraft id i want to bind to a textfield, my implementation looks like this:
FLIGHTS.filter(sql: "FLIGHT_ID = '\(currentFlightID)'").rx
.fetchOne(in: dbQueue)
.map { $0?.AC_ID ?? "" }
.bind(to: aircraftIdText.rx.text)
Any ideas on why this is the case given that I know AC_ID is not null?
Thanks!
from rxgrdb.
Any ideas on why this is the case given that I know AC_ID is not null?
Sure. The error message says that someone has tried to decode a NULL column into a non-optional String.
Even if your code only displays AC_ID, it decodes full Flight objects:
struct Flight {
var FLIGHT_ID: String // requires non-null FLIGHT_ID column
var AC_ID: String // requires non-null AC_ID column
var PILOT_ID: String // requires non-null PILOT_ID column
var NAME: String // requires non-null NAME column
...
}
There are only two ways out:
-
Fix the content of the database: add NOT NULL constraints on columns that must not be null, and find the bug which inserts those invalid nulls.
-
Make optional properties that should accept null columns:
struct Flight { var FLIGHT_ID: String // mandatory var AC_ID: String // mandatory var PILOT_ID: String? // Pilot may not be known yet var NAME: String // mandatory ... }
from rxgrdb.
Yes, I felt a bit nervous writing it like that! It is all functioning as expected now, this library has saved me pages of code! Thanks for writing it 😄
from rxgrdb.
Related Issues (20)
- RxGRDB doesn't fetch results in Test Target HOT 2
- Read in another thread is blocked using database pool. HOT 12
- Testing a fetch call within a Promise leads to unwanted results. HOT 12
- Joined requests HOT 2
- why not support carthage? HOT 1
- How to handle requests that require write access? HOT 12
- ReactiveKit support HOT 7
- Should RxGRDB provide observable for migrations? HOT 2
- Does RxGRDB work with GRDBPlus? HOT 11
- Difference fields in database and data model HOT 1
- RxGRDB backward compatibility v0.11 to v0.12.1 Cocoapods HOT 5
- Experiencing slowness in queries when upgrading from 0.12.0 to 0.13.0 HOT 2
- On which Thread the actual fetching of the data is done ? HOT 7
- Support for Swift Package Manager HOT 4
- No longer compiles with GRDB 4.1.0 HOT 4
- Linker issue when also using SQLCipher HOT 11
- Cannot compile 1.0.0-beta.2 HOT 2
- Fix podspec SPM support status HOT 1
- Compiling for iOS 10.0, but module 'GRDB' has a minimum deployment target of iOS 11.0 HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxgrdb.