I am trying to use SQLite.swift sample code but it is throwing an error.
First step I have done is to install it using cocoapods and it was successful. Then I have tried to test it in my app delegate where I declare import SQLite and I have added the below code in didFinishLaunchingWithOptions but it is showing an error.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let db = try Connection("path/to/db.sqlite3")
let users = Table("users")
let id = Expression<Int64>("id")
let name = Expression<String?>("name")
let email = Expression<String>("email")
try db.run(users.create { t in
t.column(id, primaryKey: true)
t.column(name)
t.column(email, unique: true)
})
return true
}
The error I am getting from this code line (let db = try Connection("path/to/db.sqlite3")) is "Errors thrown from here are not handled"
Is there anyone experiencing the same issue? I am using XCode 8.2.1 and Swift 3.0.
Your app needs to know what it should do when there is a runtime error. What you're missing is a do/catch clause or the throws keyword at your function declaration. For more details have a look at chapter "Error Handling" of the the Swift book.
Thanks to #martin-r for the solution. Here's the updated code.
Solution:
do {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let db = try Connection("\(documentsPath)/db.sqlite3")
let users = Table("users")
let id = Expression<Int64>("id")
let name = Expression<String?>("name")
let email = Expression<String>("email")
try db.run(users.create { t in
t.column(id, primaryKey: true)
t.column(name)
t.column(email, unique: true)
})
} catch {
// Handle error
}
Related
According to the:
Proper Realm usage patterns/best practices
What is the best practice or design pattern to maintain sync activity across multiple views
Design Pattern for Realm Database Swift 3.1 - Singleton
my approach is like:
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
DispatchQueue.main.async {
let username = "test#test.com"
let password = "Test123"
let serverUrl = URL(string: "http://test.com:9080")
let realmUrl = URL(string: "realm://test.com:9080/~/realmtest")
if let user = SyncUser.current {
Realm.Configuration.defaultConfiguration.syncConfiguration = SyncConfiguration(user: user, realmURL: realmUrl!)
} else {
SyncUser.logIn(with: .usernamePassword(username: username, password: password, register: false), server: serverUrl!, onCompletion: { (user, error) in
guard let user = user else {
print("Error: \(String(describing: error?.localizedDescription))")
return
}
Realm.Configuration.defaultConfiguration.syncConfiguration = SyncConfiguration(user: user, realmURL: realmUrl!)
})
}
}
return true
}
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
print("SyncConfiguration: \(String(describing: Realm.Configuration.defaultConfiguration.syncConfiguration))")
self.realm = try! Realm()
}
When I open app for the first time nothing happens but when I open app the second time, Realm works fine.
Whenever I open app, the printed SyncConfiguration is nil. No errors!
Searched here and there and can't find an answer...
The problem is that you are using an async method to configure your Realm, but you don't call the print inside the completion handler of your method. You should only present your viewcontoller once your asynchronous call has finished.
i am trying to retrieve data from parse local stat store.
i assume that it is pinning because there is no error when running the following:
let contact = PFObject(className: "temp")
contact["firstName"] = "steve"
contact["lastName"] = "smith"
contact["email"] = "steve.smith#example.com"
contact.pinInBackground()
the error comes when trying to retrieve:
let query = PFQuery(className: "temp")
query.whereKey("firstName", equalTo: "steve")
query.fromLocalDatastore()
query.findObjectsInBackground { (object, error) in
if error == nil {
for object in object! {
print(object["firstName"] as! String)
}
}
}
returns this error:
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Method requires Pinning
enabled.'
I've read a bit online about the call location of
Parse.enableLocalDatastore()
in the app delegate but i haven't been able to produce any results and this template was dl direct from parse, this is the first part of my app delegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//--------------------------------------
// MARK: - UIApplicationDelegate
//--------------------------------------
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Enable storing and querying data from Local Datastore.
// Remove this line if you don't want to use Local Datastore features or want to use cachePolicy.
Parse.enableLocalDatastore()
let parseConfiguration = ParseClientConfiguration(block: { (ParseMutableClientConfiguration) -> Void in
ParseMutableClientConfiguration.applicationId = "XXX"
ParseMutableClientConfiguration.clientKey = "XXX"
ParseMutableClientConfiguration.server = "XXX"
})
Parse.initialize(with: parseConfiguration)
Sorted....
Parse.enableLocalDatastore()
seems to be a legacy item that i got from somewhere.
simply adding:
ParseMutableClientConfiguration.isLocalDatastoreEnabled = true
fixes the problem.
You will need to remove or comment out all instances of
Parse.enableLocalDatastore()
I'm new to realm, and so as I was fooling around with it to learn it, I found something quite interesting. In my appDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.Hobo.RealmDatabase")!
var config = Realm.Configuration()
config.fileURL = directory.filePathURL?.URLByAppendingPathComponent("db.realm")
Realm.Configuration.defaultConfiguration = config
let realm = try! Realm()
print("File Location: \(realm.configuration.fileURL!)") // -> Location A
print("NO OF USERS: \(realm.objects(User).count)") // -> 0
return true
}
but in my ViewController:
let realm = try! Realm()
override func viewDidLoad() {
super.viewDidLoad()
print("NO OF USERS IN VIEWDIDLOAD: \(realm.objects(User).count)") // -> 1
let firstTime = loadFirstTime()
if firstTime {
// configure USER!
let user = User()
user.monthlyIncome = 50000
try! realm.write({
realm.add(user)
})
saveFirstTime(false)
print("First time, user written")
}
dailyLimit.text = String(realm.objects(User).first!.dailyLimit)
}
Notice the returns from the print() functions. In app delegate, the result of the print(number of users:) returns 0, but in the viewController's viewDidLoad, it returned a 1.
Isn't both supposed to return the same value? In this case 1?
Thanks in advance!!
Yes it is the same, I'm guessing you removing by mistake the user, on application load, or something like that, you should use "Realm browser" to check your DB state, that way you can see when an object changes during run time. https://github.com/realm/realm-browser-osx
EDIT
Check your accessing the default configuration. In realm you can have multiple configurations like so:
let config = Realm.Configuration(
// Get the URL to the bundled file
fileURL: NSBundle.mainBundle().URLForResource("MyBundledData", withExtension: "realm"),
// Open the file in read-only mode as application bundles are not writeable
readOnly: true)
// Open the Realm with the configuration
let realm = try! Realm(configuration: config)
depend on documentation of realm 3
https://realm.io/docs/swift/latest/#realm-configuration
func setDefaultRealmForUser(username: String) {
var config = Realm.Configuration()
// Use the default directory, but replace the filename with the username
config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("\(username).realm")
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
}
I am a beginner in iOS development and I want to develop a database in my application. However, When I try to create the database it says unable to open database file.
For clarification (https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md)
My code for the db development is as follow
static func DB(){
do {
let db = try Connection("db.sqlite3")
let users = Table("users")
let id = Expression<Int64>("id")
let name = Expression<String?>("name")
let email = Expression<String>("email")
try db.run(users.create { t in
t.column(id, primaryKey: true)
t.column(name)
t.column(email, unique: true)
})
} catch {
print("Dim background error")
}
}
I trigger this method from app delegate file
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
AppCommon.DB()
return true
}
How to fix this issue? Please help me out, I really need your help..!
On iOS, you can't create files where you want. A correct place (among others) is in the documents directory.
Quoting the documentation of the library you are using https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md#connecting-to-a-database:
On iOS, you can create a writable database in your app’s Documents directory.
let path = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true
).first!
let db = try Connection("\(path)/db.sqlite3")
Swift 5
Path of Documents directory is now an array of strings
let path = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true)
let db = try Connection("\(path.first ?? "")/db.sqlite3")
I'm developing an application for iOS using swift and chose Realm as a database solution for it. I wrote default data in AppDelegate using write/add function from realm docs and it works just fine. So after first launch I have a *.realm file with my initial data. In Realm documentation I found a section called "Bundling a Realm with an App", I add my *.realm file to project and to Build Phases as it written.
And I can't understand what I should do next (and part about compressing a *.realm file). I've tried to understand a code from Migration Example but I don't know Obj-C well.
Please give as clear steps as you can to add *.realm file with initial data to swift ios project and load this data to the Realm db with the first launch.
Implement this function openRealm in AppDelegate and call it in
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
openRealm()
return true
}
func openRealm() {
let defaultRealmPath = Realm.defaultPath
let bundleReamPath = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("default.realm")
if !NSFileManager.defaultManager().fileExistsAtPath(defaultRealmPath) {
NSFileManager.defaultManager().copyItemAtPath(bundleReamPath!, toPath: defaultRealmPath, error: nil)
}
}
It will copy your realm file that you bundled in the app to the default realm path, if it doesn't exist already. After that you use Realm normally like you used before.
There's also the Migration example that you talked about in Swift.
In Swift 3.0.1 you may prefer this:
let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
let bundleRealmPath = Bundle.main.url(forResource: "seeds", withExtension: "realm")
if !FileManager.default.fileExists(atPath: defaultRealmPath.absoluteString) {
do {
try FileManager.default.copyItem(at: bundleRealmPath!, to: defaultRealmPath)
} catch let error {
print("error copying seeds: \(error)")
}
}
(but please be careful with the optionals)
Swift version 3, courtesy of Kishikawa Katsumi:
let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm")
if !FileManager.default.fileExists(atPath: defaultRealmPath.path) {
do
{
try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: defaultRealmPath.path)
}
catch let error as NSError {
// Catch fires here, with an NSError being thrown
print("error occurred, here are the details:\n \(error)")
}
}
And for those that need #pteofil's answer in Objective-c
- (void)openRealm {
NSString *defaultRealmPath = [RLMRealm defaultRealm].path;
NSString *bundleRealmPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"default.realm"];
if(![[NSFileManager defaultManager] fileExistsAtPath:defaultRealmPath]) {
[[NSFileManager defaultManager] copyItemAtPath:bundleRealmPath toPath:defaultRealmPath error:nil];
}
}
Updating #pteofil's openRealm function for Swift 2.2/Realm 1.0.2:
func openRealm() {
let defaultURL = Realm.Configuration.defaultConfiguration.fileURL!
let bundleReamPath = NSBundle.mainBundle().URLForResource("default", withExtension: "realm")
if !NSFileManager.defaultManager().fileExistsAtPath(defaultURL.path!) {
do {
try NSFileManager.defaultManager().copyItemAtURL(bundleReamPath!, toURL: defaultURL)
}
catch {}
}
}
Work in the enterprise space, I need to open a Realm for each application without reusing Realm across all applications so I put this together for Swift 3.0. Add this function to the AppDelegate.
func openRealm()
{
let appName = "ApplcationNameGoesHere"
var rlmConfig = Realm.Configuration()
let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
let appRealmPath = defaultRealmPath.deletingLastPathComponent().appendingPathComponent("\(appName).realm")
if !FileManager.default.fileExists(atPath: appRealmPath.path) {
// Use the default directory, but replace the filename with the application name: appName
rlmConfig.fileURL = rlmConfig.fileURL!.deletingLastPathComponent().appendingPathComponent("\(appName).realm")
}else
{
rlmConfig.fileURL = appRealmPath
}
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = rlmConfig
}// open the Realm database for the application
The code above opens or creates a Realm with the file name of "ApplicationNameGoesHere.realm" based on the appName variable in this example.
place
openRealm() before return true in application: didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
openRealm()
return true
}
call it in another class like this:
let uiRealm = try! Realm()
If you want to open it straight from the bundle location and not bother copying it to the default Realm path, look at the implementation here
Download Realm Studio in your system. Then print the path from Xcode and copy it:
print(Realm.Configuration.defaultConfiguration.fileURL!)
Then open the terminal and write:
open //file path
It will open the file in Realm Studio and you can see your model data there.