Inserting initial data after database creation in SQLite.swift - ios

I have a SQLite database that I want to populate with some initial data.
Using SQLite.swift, this method seems to be working:
do {
let _ = try db.run( userDictionary.create(ifNotExists: false) {t in
t.column(wordId, primaryKey: true)
t.column(word, unique: true)
t.column(frequency, defaultValue: 1)
t.column(following, defaultValue: "")
})
} catch _ {
print("table already exists")
return
}
// Add some initial data for a new database
print("adding new data")
myMethodToInsertInitialData()
The way this works, though is that I am using ifNotExists: false to throw an error every time after the initial database creation. (Setting it to true would not throw an error (or allow the early return).) However, throwing an error on purpose every time except the first time seems like poor programming. I don't really mean it as an error. I just want to insert some data in a newly created database. Is there a better way to do this or is this what everyone does?

To check whether data is available in table or not in SQLite.swift
do
{
db = try Connection(YOUR_DB_PATH)
let intCount : Int64 = db.scalar("SELECT count(*) FROM yourTableName") as! Int64
if (intCount == 0)
{
//callWebServiceCall()
}
else
{
//getDataFromDB()
}
}
catch
{
print("error")
}

Related

Listener event not triggered when document is updated (Google Firestore)

I am struggling to understand why my event listener that I initialize on a document is not being triggered whenever I update the document within the app in a different UIViewController. If I update it manually in Google firebase console, the listener event gets triggered successfully. I am 100% updating the correct document too because I see it get updated when I update it in the app. What I am trying to accomplish is have a running listener on the current user that is logged in and all of their fields so i can just use 1 global singleton variable throughout my app and it will always be up to date with their most current fields (name, last name, profile pic, bio, etc.). One thing I noticed is when i use setData instead of updateData, the listener event gets triggered. For some reason it doesn't with updateData. But i don't want to use setData because it will wipe all the other fields as if it is a new doc. Is there something else I should be doing?
Below is the code that initializes the Listener at the very beginning of the app after the user logs in.
static func InitalizeWhistleListener() {
let currentUser = Auth.auth().currentUser?.uid
let userDocRef = Firestore.firestore().collection("users").document(currentUser!)
WhistleListener.shared.listener = userDocRef.addSnapshotListener { documentSnapshot, error in
guard let document = documentSnapshot else {
print("Error fetching document: \(error!)")
return
}
guard let data = document.data() else {
print("Document data was empty.")
return
}
print("INSIDE LISTENER")
}
}
Below is the code that update's this same document in a different view controller whenever the user updates their profile pic
func uploadProfilePicture(_ image: UIImage) {
guard let uid = currentUser!.UID else { return }
let filePath = "user/\(uid).jpg"
let storageRef = Storage.storage().reference().child(filePath)
guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }
storageRef.putData(imageData) { metadata, error in
if error == nil && metadata != nil {
self.userProfileDoc!.updateData([
"profilePicURL": filePath
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
}
}
}
You can use set data with merge true it doesn't wipe any other property only merge to specific one that you declared as like I am only update the name of the user without wiping the age or address
db.collection("User")
.document(id)
.setData(["name":"Zeeshan"],merge: true)
The answer is pretty obvious (and sad at the same time). I was constantly updating the filepath to be the user's UID therefore, it would always be the same and the snapshot wouldn't recognize a difference in the update. It had been some time since I had looked at this code so i forgot this is what it was doing. I was looking past this and simply thinking an update (no matter if it was different from the last or not) would trigger an event. That is not the case! So what I did was append an additional UUID to the user's UID so that it changed.

SQLite select query error unrecognized token ":"

I have to update the structure of my exiting table, already done in past. Now, I delete and recreate table to add some columns, but when I query db, I obtain the error -> unrecognized token: ":"
But db and all tables are empty. I don't understand what going wrong.
I'm trying to modify, update code, change query table but obtain the same result.
var settings = Table('settings')
if let db = getDB() {
do{
try db.run(settings.drop(ifExists: true)) //OK
try db.run(settings.create { t in
t.column(id, primaryKey: .autoincrement)
t.column(Expression<String?>("email"), defaultValue: nil)
t.column(Expression<String?>("token"), defaultValue: nil)
}) //OK
try db.scalar(settings.count) // ERROR
} catch {
throw error
}
}

Search data from Realm jammed the main thread

I got an old project that has a local record search algorithm based on Realm database.
The main thread will be jammed when the search began in the database that has thousands of records.
It can't be switched to other threads since the existed Realm database created and run on the main thread.
Is there anyone knows how to solve this jam problem?
Thanks in advance.
# The search algorithm is actually only applied a filter on the objects array.
# data is all items in database
data = realm.objects(CustomObject.self).filter(filterPredicate(parentID: id, keyword: keyword, colorIndex: colorIndexes, isActionOnly: True)).sorted(by: descSorting)
private func filterPredicate(parentID: Int?, keyword: String?, colorIndex: [Int]?, isActionOnly: Bool = false) -> InspirationFilterClosure {
return { item in
if item.isDeleted { return false }
if let id = parentID, item.parentID != id { return false }
else if item.parentID < 0 { return false }
if isActionOnly, !item.isAction { return false }
if let indexes = colorIndex, !indexes.contains(-1), !indexes.contains(item.colorIndex) { return false }
if let keyword = keyword, !keyword.isEmpty {
return item.content.range(of: keyword, options: .caseInsensitive) != nil
}
return true
}
}
You have 2 options to avoid Jamming of main Thread
You have to use this concept ThreadSafeReference which is mentioned in the documentation.
You can use Realm Notifications concept to observe your fetched objects in background thread.
Suggestions: Please go through above concepts and try to implement it on your own. if you still face any issue, do let us know.

why I still get deleted documents when getting data using listener from Firestore in Swift?

I trying to make an event app. and I add a new field in my events documents. I try to add "venue" field for my event documents
so before I run the app, I delete all the available data on my Firestore database. But when I retrieve my data back to the app, it is said that the "venue" is nil, it seems that the "venue" field is not exist, even though in fact, the "venue" field exist on my firestore database.
I suspect my app still retrieve my deleted documents. here is why
here is the code I use :
enum FirestoreCollectionReference {
case users
case events
case cities
case APIKey
private var path : String {
switch self {
case .users : return "users"
case .events : return "events"
case .cities : return "cities"
case .APIKey : return "secretAPIKeyKM"
}
}
func reference () -> CollectionReference {
return Firestore.firestore().collection(path)
}
}
FirestoreCollectionReference.events.reference()
.whereField("city", isEqualTo: selectedCity)
.whereField("eventType", isEqualTo: selectedEventType)
.whereField("coordinate", isGreaterThan: lesserGeopoint)
.whereField("coordinate", isLessThan: greaterGeopoint)
.order(by: "coordinate")
.order(by: "dateTimeStart", descending: true)
.limit(to: 20)
.addSnapshotListener { (snapshot, error) in
if let error = error {
completion(nil,eventListener)
print("Error when observing events document: \(error.localizedDescription)")
} else {
print("Successfully get events data from Firestore by Listener")
guard let documentsSnapshot = snapshot else {
completion(nil, eventListener)
return
}
let eventDocuments = documentsSnapshot.documents
print("the number of documents: \(eventDocuments.count)")
var eventsArray = [EventKM]()
for document in eventDocuments {
let eventDictionary = document.data()
let theEvent = EventKM(dictionary: eventDictionary)
eventsArray.append(theEvent)
}
completion(eventsArray,eventListener)
}
}
}
I try to print the number of documents, and it shows that I have 8 documents from this query, in fact, it should be only one document available in my database.
I try to delete the composite indexes from firebase console, but usually, after I delete the composite indexes, It will give an error + a link to generate the composite indexes in my debugging area on my Xcode, but after I delete the composite indexes, I don't get the error + link to generate the indexes, and give 8 documents (it should be one document only)
it seems the data is cached on my iOS app. isn't it? or is this a bug since Firestore is still in Beta version? I need to understand why and how to solve this issue so I can understand firebase better. Thanks in advance.
FireBase has cached in device so if user stay in outside of internet,
But still can use Firebase.
So You just remove your app in your simulator.
In my case, It fixed.

PFQuery always return same results even though Parse server changed

I'm developing an iOS project using Parse.com as backend server.
Basically, I'm currently implementing a very basic feature which just simply retrieve some objects with simple condition.
However, the objects can only be correctly retrieved the first time. No matter how I changed any values in Parse "Core" via Web, I still cannot get updated values by refreshing in the app.
For example, I have a class called "Event", the fields are changed from Parse server, but the result I retrieve are never updated.
let eventServerQuery = Event.query()
// I tried to clear all cached results
PFQuery.clearAllCachedResults()
eventServerQuery?.whereKey(EventFields.Campus.rawValue, equalTo: campus!)
eventServerQuery?.findObjectsInBackgroundWithBlock({ (allEvents, error) -> Void in
self.refreshControl?.endRefreshing()
self.toggleRefreshButtonWithSpinner(false)
if error != nil {
print(error?.localizedDescription)
}else{
if allEvents?.count > 0 {
// Display on the map
for eventObject in allEvents! {
let event = Event.initializeFieldsFromPFObject(eventObject)
self.delegate?.addEventToMap(event)
self.events.append(event)
print("\(event.updatedAt)")
print("\(event.title) has \(event.numberOfTasks) tasks")
}
// Event TVC data source
self.tableView.reloadData()
}
}
})
If I delete the app in my device and run the project again, it will of course reload everything from scratch, so that the data will become correct again...
Any help will be appreciated!
Finally, I worked out by myself. I found that whenever the PFObject was pinned, its fields will not be updated. The solution is that the object need to be unpinned before retrieve from server.
Event.unpinAllInBackground(events, block: { (success, error) -> Void in
if error != nil {
print(error?.localizedDescription)
}else{
self.events.removeAll()
let eventServerQuery = Event.query()
eventServerQuery?.whereKey(EventFields.Campus.rawValue, equalTo: self.campus!)
eventServerQuery?.findObjectsInBackgroundWithBlock({ (allEvents, error) -> Void in
print("Debug: retrieving events from server")
self.refreshControl?.endRefreshing()
self.toggleRefreshButtonWithSpinner(false)
if error != nil {
print(error?.localizedDescription)
}else{
if allEvents?.count > 0 {
// Display on the map
for eventOnline in allEvents! {
let event: Event = eventOnline as! Event
event.pinInBackground()
self.delegate?.addEventToMap(event)
self.events.append(event)
}
// Event TVC data source
self.tableView.reloadData()
}
}
})
}
})
Welcome to add comments here regarding the internal logic of Parse library, as sometimes it is not quite clear I think.

Resources