I am fetching data from FireBase and save it inside my realm but it is not working as expected :
for doc in docs {
let shopData = doc.data()
let newShop = RLMShop()
newShop.shopName = shopData["name"] as? String ?? "Empty Name"
self.saveShop(shop: newShop) // My issue is here
}
My saveShop function :
func saveShop(shop: RLMShop) {
do {
try realm.write {
realm.add(shop)
}
} catch {
print("Error saving shop \(error)")
}
}
Calling save function is not saving my object.
The problem you have is that you are creating a RLMShop object but it is not linked to a RLMShopsCategory object, therefore your shopsList will not contain the new object.
// Fetch the RLMShopsCategory that you wish to add the RLMShop too
// Using Primary Key here just as an example
let shopsCategory = realm.object(ofType: RLMShopsCategory.self, forPrimaryKey: "YourKey")
for doc in docs {
let shopData = doc.data()
let newShop = RLMShop()
newShop.shopName = // setting the properties etc
// This is the part you are missing
// You need to append the newShop to your shopsCategory object
try! realm.write {
shopsCategory.shopsList.append(newShop)
}
}
Related
I am saving items in UserDefaults and it works fine. I simply append new elements to the array. now deleting the entire saved items is done but now I want to enable the user the ability to delete just one item instead of deleting the entire saved items.
below is how I delete all the entire array
public func deleteSavePropery() {
delete(key: propertyKey)
}
private func delete(key: String) {
storage.removeObject(forKey: key)
}
NOTE, saveProperty is a Codable object
You need to retrieve the array if exists then delete the item finally save back
let storage = UserDefaults.standard
private func deleteItem(key: String,item:Item) {
if let data = storage.data(forKey: key) , var arr = try? JSONDecoder().decode([Item].self, from: data) {
arr.removeAll(where: { $0 == item})
guard let res = try? JSONEncoder().encode(arr) else { return }
storage.set(res, forKey: key)
}
}
struct Item:Codable,Equatable {
let name:String
}
You need to,
Fetch the array from UserDefaults.
Remove the element from the array based on your condition.
Save the array back to UserDefaults.
Example-1: removing the element from array based on the index.
func remove(at index: Int) {
if let data = storage.data(forKey: "YOUR_KEY") {
do {
var arr = try JSONDecoder().decode([Item].self, from: data)
arr.remove(at: index)
let data = try JSONEncoder().encode(arr)
storage.set(data, forKey: "YOUR_KEY")
} catch {
print(error)
}
}
}
Example-2: removing a particular element from array.
func remove(element: Item) {
if let data = storage.data(forKey: "YOUR_KEY") {
do {
var arr = try JSONDecoder().decode([Item].self, from: data)
arr.removeAll { $0 === element }
let data = try JSONEncoder().encode(arr)
storage.set(data, forKey: "YOUR_KEY")
} catch {
print(error)
}
}
}
UserDefaults doesn't provide any API to manage the contents of a particular key. In fact, it doesn't care what kind of content is saved there.
So, what you need to do is: extract the array from UserDefaults, edit it and then save an updated array to UserDefaults.
I've recently changed a lot on my iOS application and now I got stuck.
I'm trying to insert data from Firestore which looks like this:
So, as you can see I've 6 different names in here.
And here is the code to insert into pickerView.
func getPerson()
{
let authentication = Auth.auth().currentUser?.uid
db.collection("users").document(authentication!).collection("person").getDocuments { (QuerySnapshot, err) in
//If error is not equal to nil
if err != nil
{
print("Error getting documents: \(String(describing: err))");
}
//Succeded
else
{
//For-loop
for _ in QuerySnapshot!.documents
{
//Cleaning the array for the new values
self.personArray.removeAll()
let document = QuerySnapshot!.documents
let data = document.data() //HERE IS THE ERROR
data.forEach { (item) in
if let person1Data = data["name"] as? String
{
self.personArray.append(person1Data)
print(self.personArray)
}
}
}
self.pickerView.reloadAllComponents()
}
}
}
I'm getting the error:
Value of type '[QueryDocumentSnapshot]' has no member 'data'
It used to have QuerySnapshot!.documents.first
but it does not work anymore when I've changed the Firestore data.
Edit:
So. the output is now:
["Joche"] ["Joche", "Joche"] ["Putte"] ["Putte", "Putte"] ["Rebecca"]
["Rebecca", "Rebecca"] ["Fredrik"] ["Fredrik", "Fredrik"] ["Anna"]
["Anna", "Anna"] ["Vickan"] ["Vickan", "Vickan"]
which means it adds everything but x3. How to solve this problem?
data is an instance method of a single QueryDocumentSnapshot not an array , You need
self.personArray.removeAll()
for elem in querySnapshot!.documents {
let data = elem.document.data()
data.forEach {
if let person1Data = $0["name"] as? String {
self.personArray.append(person1Data)
print(self.personArray)
}
}
}
I have products in my Realm database like this
I want to update my realm database based on productID, so I don't need to add another product over and over again. let say I want to update quantity of product that has productID = "a" to be 5.
I have tried to write something like this.
let selectedProductID = "a"
let productsInRealmDatabase = realm.objects(Product.self)
let productIndex = productsInRealmDatabase.index(where: {$0.productID == selectedProductID})
if let productIndex = productIndex {
do {
try realm.write {
var productRealm = productsInRealmDatabase[productIndex]
productRealm.quantity = 5
productsInRealmDatabase[productIndex] = productRealm // ERROR HERE
}
} catch {
// error Handling
}
}
but I got error in : productsInRealmDatabase[productIndex] = productRealm
Error Message: Cannot assign through subscript: subscript is get-only
so how to update realm object based on the certain property in Realm?
You should use Realm's own filter method which accepts an NSPredicate and returns an auto-updating Results instance rather than Swift's filter when operating on Realm collections. Than either update the properties of the fetched prouduct or create a new one and save that to Realm.
let selectedProductID = "a"
let productsInRealmDatabase = realm.objects(Product.self)
let matchingProduct = productsInRealmDatabase.filter("productID == %#", selectedProductID).first
if let matchingProduct = matchingProduct {
do {
try realm.write {
matchingProduct.quantity = 5
}
} catch {
// error Handling
}
} else {
let newProduct = Product()
newProduct.productID = selectedProductID
newProduct.quantity = 5
do {
try realm.write {
realm.add(newProduct)
}
} catch {
// error Handling
}
}
If you want your Products to be unique based on their productID property, you use also set productID as the primaryKey of your Object subclass.
class Product:Object {
#objc dynamic var productID = ""
...
override static func primaryKey() -> String? {
return "productID"
}
}
Try this -
let selectedProductID = "a"
let productsInRealmDatabase = realm.objects(Product.self)
let filteredProducts = productsInRealmDatabase.filter("productID = \(selectedProductID)")
do {
try realm.write {
filteredProducts.forEach { product in
product.quantity = 5
}
}
} catch {
// error Handling
}
While inserting data to your database inside the insert function mark update key as true and then try updating the value. eg:
static func insertData() {
//Your insertion code//
try! realm.write {
realm.add(request, update: true)
}
}
static func updateData(productId: String, quantity: Int) {
let product = self.getProductData(prodId: productId)
let realm = try! Realm()
try! realm.write {
product?.quantity = quantity
}
}
Hope this helps you out.
I am trying to remove an element from a list which is stored in NSUserDefaults. The getAll function is implemented below:
func getAllOrders() -> [Order] {
var orders = [Order]()
if let userDefaults = UserDefaults(suiteName: "group.com.johndoe.SoupChef.Shared") {
if let ordersData = userDefaults.data(forKey: "Orders") {
orders = try! JSONDecoder().decode([Order].self, from: ordersData)
}
}
return orders
}
And here is the code for deleting the order.
func delete(order :Order) {
var persistedOrders = getAllOrders()
persistedOrders.removeAll { persistedOrder in
persistedOrder.identifier.uuidString == order.identifier.uuidString
}
}
After deleting the order in the code above when I call getAllOrders I still see all the elements, meaning I don't see the order being deleted.
That's because you don't save your changes. Once you've performed the removal you need to turn persistedOrders back into JSON and then:
userDefaults.set(json, forKey:"Orders")
You need to use jsonEncoder and encode the edited array then store it again the user defaults
func delete(order :Order) {
var persistedOrders = getAllOrders()
persistedOrders.removeAll { persistedOrder in
persistedOrder.identifier.uuidString == order.identifier.uuidString
}
do {
let data = try JSONEncoder().encode(persistedOrders)
userDefaults.set(data, forKey:"Orders")
}
catch {
print(error)
}
}
You have to store correctly your UserDefaults
UserDefaults.standard.set(json, forKey:"Orders")
Now, you can remove them using:
UserDefaults.standard.removeObject(forKey: "Orders")
Ok, so I have a Realm database (named AutoComplete) with the following structure
I
Now I do a call to the backend, to get make all the [Folder] items.
Alamofire.request(.GET, URL).responseObject { (response: Response<Folders, NSError>) in
if response.result.isSuccess {
let mappedObject = response.result.value
let realm = try! Realm()
// open AutoComplete model
let openAutoComplete = realm.objects(AutoComplete)
try! realm.write {
if let everythingUnderResult = mappedObject?.result {
// for folder in [Folder]
for item in everythingUnderResult {
//check if the search string you typed in exists in the database
let searchifExists = openAutoComplete.filter("search == %#", searchString)
let makeString = searchifExists.first
let getSearchColumn = makeString?.search
if searchString == getSearchColumn {
//item exists, now add the folder to the autocomplete DB
makeString?.folder.append(item)
realm.add(makeString!,update: true)
} else {
print(item)
realm.add(item)
makeString?.folder.append(item)
}
}
}
}
} else {
print("couldn't get mobileapp data")
}
}
}
My issue is that I can't add the folderobjects to the database. The makeString?.folder.append(item) line of code gives back the following error:
Can't set primary key property 'id' to existing value '65502'.'
I know that the id already exists in my Folders model of the database, with the same ID, but I just want to reference it in the Autocomplete model. (not update it or overwrite it).
Anyone has an idea on how to fix this?
In this part of code
if searchString == getSearchColumn {
//item exists, now add the folder to the autocomplete DB
makeString?.folder.append(item) // it's ok
realm.add(makeString!,update: true) // you don't need this
} else {
print(item)
realm.add(item)
makeString?.folder.append(item)
}
just remove
realm.add(makeString!,update: true)
You don't need to add this record one more time to the table.Adding it to the folder list is enough