RealmSwift. Difficulties initializing List after converting to Swift 4 - swift

I've got a task to convert an existing project to Swift4 (from Swift 2.3, don't ask O_o). Managed to fix almost everything except this issue using RealmSwift:
Old code:
class func myFunc() -> List<MyClass> {
let realm = try! Realm()
return List(realm.objects(MyClass).sorted("name", ascending: true))
}
Getting compiler error for the return statement:
Argument passed to call that takes no arguments
When I trying to fix like this, compiler is silent, but the function doesn't do its job:
return List<MyClass>()
So which is then the right way to initialize List with a collection of custom objects? Please, help!

List doesn't have an initializer accepting a Results instance in RealmSwift 3.1.0 (I'm not sure since when though). List's only initializer doesn't take any input arguments and it creates an empty List instance.
You can work around this by creating an empty List using the only initializer, then calling append to add the elements of the Results collection to the list.
func myFunc() -> List<MyClass> {
let realm = try! Realm()
let list = List<MyClass>()
list.append(objectsIn: realm.objects(MyClass.self).sorted(byKeyPath: "name", ascending: true))
return list
}

Related

Contextual closure type firebase / swift

Somewhat confused by this error, very new to Firebase but I think this is more a swift programming issue on my part. A lot of the swift coding I'm trying to implement with firebase is new to me.
"Contextual closure type '(Result<StorageListResult, any Error>) -> Void' expects 1 argument, but 2 were used in closure body"
Starting with:
textRef.listAll { (result, error) in
for item in result!.items {
If I use 'item' within this closure alone such as:
let downloadTask = item.write(toFile: localURL) { url, error in
if let error = error {
print ("UNABLE TO DOWNLOAD FILES")
} else {
print("NEW FILE DOWNLOADED")
print(item)
}
}
Works perfectly.
But if I try and use item a 2nd time within the closure, such as:
let serverTimestamp = dateFormatter.string(from: itemTemp.getMetadata.updated())
I get the above error.
The aim of my code is to gain a list all items on my Firebase storage, then against each storage item firstly check its metadata updated date before deciding whether to download or not. However within the closure I can't seem to use item more than once. I either check its metadata or download...not both.
I've tried looking at closures, but struggling to see how I could potential expand the closure to incorporate what I want.
Any advice apprecaited

No exact matches in call to initializer when initializing Data in AppStorage

I'm learning how to store custom types in AppStorage, and came across an issue. In this simplified example, I'm trying to save an empty Int array to AppStorage once the view is created.
The following code gives me the error, No exact matches in call to initializer . I know that this error usually means there are mismatching types somewhere, but I'm not sure what the types should be, or how to fix it.
struct test: View {
init() {
let emptyList = [Int]()
guard let encodedList = try? JSONEncoder().encode(emptyList) else { return }
self.storedList = encodedList
}
#AppStorage("stored_list") var storedList: Data //NO EXACT MATCHES TO CALL IN INITIALIZER
//"body" implementation not shown
}
Why is this error occurring, and how can I fix it?
It should be either with default value or optional, so correct variants are
#AppStorage("stored_list") var storedList: Data = Data()
or
#AppStorage("stored_list") var storedList: Data?

Modifying Realm values swift using map

I want to modify model data in Realm database in Swift App. Here is my code:
try! realm.write {
realm.objects(CompanyModel.self).map({ (model) in
model.isSelected = true
})
}
Idea is simple, iterate through models and change isSelected bool property to true. But look like it not work. Why?
You shouldn't use map if you want to modify the original Results instance and hence the model objects stored in your Realm. 'map' is not a mutating function, it iterates through your array (or in this case, Results), applies a transformation to each element of the sequence, then returns a new sequence containing the results of the transformation.
What you would actually need is the forEach function, which only iterates through the elements of an Array and executes the closure for each element,but doesn't return a new Array. I am currently not able to test it, but I since forEach is a member function of Array and not of NSFastEnumeration, from which Results inherits, I think you cannot use forEach on a Results instance, so you need to use a regular for loop to do this.
try! realm.write {
for model in realm.objects(CompanyModel.self) {
model.isSelected = true
}
}

LazyFilterBidirectionalCollection<Results<datatype>>' to expected argument type '[datatype]'

After I convert my project from swift 2.3 to swift 3 , I got this error in Realm Filter I can't get filter result in array :
func filterUsers(_ searchText: String, completion: (([Lawyer]) -> Void)) {
let bgRealm = try! Realm() // Filter the whole list of users
let results = bgRealm.objects(Lawyer.self).filter { (cc) -> Bool in
cc.name.contains2(searchText) ||
cc.name.contains2(searchText) ||
cc.phoneNumber2.contains2(searchText)
}
print(results)
completion(results)
}
filter, map, etc. are lazy operations on the Results returned by objects. This is implemented in types such as LazyFilterBidirectionalCollection<T>. To actually perform the filtering and get an array of results, you need to promote this collection to an array by wrapping in an Array initializer (e.g. Array(bgRealm.objects...))
From the docs:
Queries return a Results instance, which contains a collection of
Objects. Results have an interface very similar to Array and objects
contained in a Results can be accessed using indexed subscripting.
Unlike Arrays, Results only hold Objects of a single subclass type.
All queries (including queries and property access) are lazy in Realm.
Data is only read when the properties are accessed.

.prepare vs. .select

I have a working connection to a database in an iOS10 app, using SQLite.swift.
I want to select details for a specific university where I have an IHE_ID passed in from another view controller.
I would like to just select the row for that specific university, but I can't get a query to work. I can however loop through all the data with a prepare and then choose the one I need from that, which of course is more resource intensive than I need since I already have the specific IHE_ID passed in as anIHE Int from the sending view controller.
connection is working so omitting that code...
do {
let db = try Connection(destinationDBPath, readonly: true)
let IHEs = Table("IHE")
let IHE_ID = Expression<Int>("IHE_ID")
let IHE_Name = Expression<String>("IHE_Name")
let IHE_COE_Url = Expression<String>("IHE_COE_URL")
let IHE_Sector = Expression<Int>("IHE_Sector")
let IHE_COE_Name = Expression<String>("IHE_COE_Name")
for ihe in try db.prepare(IHEs){
if (ihe[IHE_ID] == anIHE){
// build this string, otherwise ignore rest of dataset (doing this because query isn't working)
buildIHE = "Name: \(ihe[IHE_Name])\n"
buildIHE.append("URL: \(ihe[IHE_COE_Url])\n")
// buildIHE.append("Sector: \(ihe[IHE_Sector])\n")
if (ihe[IHE_Sector] == 0) {
buildIHE.append("Sector: Public\n")
} else {
buildIHE.append("Sector: Private\n")
}
buildIHE.append("College of Education Name: \(ihe[IHE_COE_Name])\n")
}
}
print ("Got through building IHE portion of view")
What I'd like to do is use this instead of the for loop.
if let query = IHEs.select(IHE_ID,IHE_Name,IHE_COE_Url,IHE_Sector,IHE_COE_Name).filter(IHE_ID == anIHE){
print("Query successful for \(anIHE) with name \(query[IHE_Name])")
// more actions to build the string I need would then occur
} else {
print("Query has failed or returned nil")
}
Finally, I'll use the selected elements if I can get the query to work.
I think I probably just have something wrong with my syntax on the query, but any help is appreciated.
The line with the "if let query" has this error in Xcode:
Initializer for conditional binding must have Optional type, not 'Table'.
This leads me to think it's something with my use of the .select statement and just new to using SQLite.swift and swift in general.
Last thing is that anIHE comes into this function as an Int, and IHE_ID is Expression as shown in this code. I'm thinking this may be part of the problem.
The Initializer for conditional binding must have Optional type error means that the expression on the right of the if let v = expr statement is not an Optional: there is no point using if let, and the Swift compiler says that you should just write let v = expr.
And indeed, IHEs.select(...).filter(...) returns a non-optional value of type Table.
It is not the database row you expect, because the query has been defined, but not executed yet. After all, you weren't using db: where would the rows be loaded from?
The solution is to bring back the database connection, and load a single row. This is done with the pluck method.
if let ihe = try db.pluck(IHEs.select(...).filter(...)) {
print("Name \(ihe[IHE_Name])")
}