how to convert RealmSwift List to Results? - ios

I am using a Realm List/Results as my dataSource for a UITableView. At some point I assign a list to it. like:
var dataSource:List<SomeObject>! // Or >> Results<SomeObject>!
let aRealmObject = realm.objectForPrimaryKey(SomeObject.self, key: objectId)
dataSource = aRealmObject.someList // dataSource should be List
Then I have a filter on this list If the user changed the filter dates, I do like this:
dataSource = dataSource.filter("FILTER THE DATES",newDates) // dataSource should be Results
But the line above causes an error as the return type of filter is a Results object and aRealmObject.someList is a List.
What is the best way to deal with this situation?
make dataSource as a List and convert the Results object to List? How??
make dataSource as a Results and convert the List to Results? How??
Or may be you have a better way of doing it, Please share it with me.
Thanks,

I have found A simple way to convert List to Results making use if the filter method, it always returns Results object. Just gave it a true predicate.
dataSource = aRealmObject.someList.filter("TRUEPREDICATE") //this is a Results object.

Both List and Results (as well as LinkingObjects) can be converted into an AnyRealmCollection type. I think this is probably the best way to standardize all of Realm's array-type types:
var dataSource:AnyRealmCollection!
let aRealmObject = realm.objectForPrimaryKey(SomeObject.self, key: objectId)
dataSource = AnyRealmCollection(aRealmObject.someList)

Related

ParseSwift queryConstraint on object

How can i add a queryConstraint on a object?
This is my current code but it returns no objects. I guess my current code is actually to query on arrays and not objects. But I can't find a way to do this for objects.
let query = Device.query()
.where(containsString(key: "apps", substring: "Google"))
This is the database
I recommend looking at the playgrounds to see how to use ParseSwift properly. More specifically, finding objects.
The first problem is apps is an object, which is actually a dictionary. You can’t use a substring constraint on a dictionary or other object. The actual way to do it is:
let objectToFind = [“Google”: “300”]
let query = Device.query("apps" == objectToFind),

Qmetry- How to store and access Array of array list of strings

In Qmetry,Trying to save array of arraylist string and access same in another test case.
Array of array list :
ArrayList<ArrayList<String>> my_list
store(my_list, "array_list_1");
//Accessing saved list
Object list_details = getBundle().getObject(array_list_1);
System.out.println("++++ saved list details++++" + list_details);
I am able to print list_details content. Till this there is no issue. But when I try to get the first index within the arraylist, not able to use .get(0) method. Below is the code.
ArrayList<String> list_details1 = list_details.get(0);
When tried typecasting , got an error 'java.lang.String cannot be cast to java.util.ArrayList'
typecast:
ArrayList<ArrayList<String>>list_details1 = (ArrayList<ArrayList<String>>)list_details;
Need to know is it the right way to store and access arraylist ? Please suggest.
if you want store in file, you can use .csv or .xls to save ArrayList<ArrayList> my_list, if ArrayList<ArrayList> my_list you query from database, you can set as static variable, then you can use in different method

Realm and Diffable Data Source

Has anyone tried to use Realm in conjunction with diffable data sources? There seems to be an issue I can't get my head around.
So, when when we use the "traditional" data source API, in cellForRowAt we get the object for a particular row on an index-based basis and since Realm returns objects inside its generic Results type (e.g. let items = Results<Item>), which conforms to Collection, we can basically access the element like always: items[indexPath.row]
However, when you use diffable data source and create a snapshot, you have it like this: var snapshot = NSDiffableDataSourceSnapshot<Int, Item>. You add a section by doing snapshot.appendSections([0]) and then you have to add items to that section. My main issue here is that you can't do snapshot.appendItems(items) since items is of type Results<Item>, not Array<Item>. Am I missing something here?
Also, Realm's Object class seems to have its own implementation of Hashable so I think there's no way of ensuring the uniqueness of objects apart from overriding Realm's primaryKey and implementing your own auto increment feature. All that seems a bit weird to me to the point where I'm thinking to switch to Core Data.
Haven't found similar topics on StackOverflow so any help will be much appreciated.
You can covert Result<Items> to Array using an extension:
extension Results {
func toArray() -> [Element] {
return compactMap {
$0
}
}
}
You can use it like: results.toArray() and it will give you the array and then with your case you can append data into it.
Also, you can simply use this method :
let realmArray = Array(items!)
Try to use map() to transform an array
items = realm.objects(Item.self).map({ $0 })

How to access map keys through index? Dart

dartques = {'Color':[], 'Fruits':[], 'Hobbies':[]};
How to access the values using index in map?
I need to access only key or value using index.
Just like we do in list
=>list[1]
you can get it like this
var element = dartques.values.elementAt(0);
also for Dart 2.7+ you can write extension function
extension GetByKeyIndex on Map {
elementAt(int index) => this.values.elementAt(index);
}
var element = dartques.elementAt(1);
For accessing the values by index, there are two approaches:
Get Key by index and value using the key:
final key = dartques.keys.elementAt(index);
final value = dartques[key];
Get value by index:
final value = dartques.values.elementAt(index);
You may choose the approach based on how and what data are stored on the map.
For example if your map consists of data in which there are duplicate values, I would recommend using the first approach as it allows you to get key at the index first and then the value so you can know if the value is the wanted one or not.
But if you do not care about duplication and only want to find a value based on Index then you should use the second approach, as it gives you the value directly.
Note: As per this Answer By Genchi Genbutsu, you can also use Extension methods for your convenience.
Important:
Since the default implementation for Map in dart is LinkedHashmap you are in great luck. Otherwise Generic Map is known for not maintaining order in many programming languages.
If this was asked for any other language for which the default was HashMap, it might have been impossible to answer.
You can convert it to two lists using keys and values methods:
var ques = {'Color':['a'], 'Fruits':['b'], 'Hobbies':['c']};
List keys = ques.keys.toList();
List values = ques.values.toList();
print (keys);
print (values);
The output:
[Color, Fruits, Hobbies]
[[a], [b], [c]]
So you can access it normally by using keys[0], for example.

Adding a Value to Collection Items

I want to load data from many files. Each file is named with a date and I need to inject this date to each of the fetched Entries of my file.
I know I could do this with an foreach - loop before inserting the data into the collection, but I think there should be a better solution.
Content of one file
[{"price":"95,34","isin":"FR0000120073"},{"price":"113,475","isin":"CA13645T1003"}]
The Code I use to move the data into a collection.
$collection= collect(json_decode(File::get($file)));
I tried for example the "map" method, however I don't know how to pass an additional variable to the anonymous function.
The content of my collection should look like this:
[{"price":"95,34","isin":"FR0000120073","date":"2016-06-23"},{"price":"113,475","isin":"CA13645T1003","date":"2016-06-23"}]
Is there any simple solution using the collections or do I have to use a foreach-loop?
May be this will help
$collection = collect(json_decode(File::get($file)));
$collection = $collection->each(function ($item, $key) {
//First iteration of $item will be {"price":"95,34","isin":"FR0000120073"}
$item->date = "2016-06-23"; //Insert key, value pair to the collection
});

Resources