max value of string in realm - ios

I intended to get the max value of string type in Realm by maxOfProperty method as follows:
[[MyRealmObject allObjects] maxOfProperty:propertyName]
However, I encountered the following error:
maxProperty is not supported for string property
Considering I can't change the type of property, what should I do now?

maxOfProperty is only supported for int, float, double, and NSDate types, see more in docs.
There is no any builtin solution for your case, I'd suggest you to store the maximum value as a separate object and update it everytime you modify the database or just query all objects and find the maximum value by comparing it manually.

We can use this solution
[[[[MyRealmObject allObjects] sortedResultsUsingKeyPath:propertyName ascending:NO] firstObject] objectForKeyedSubscript:propertyName];

Related

core data derivation expression key path uses an operator as an intermediate component

I'm trying to write the derivation expression for the sum of a to many relationship attribute.
I have an item and a group, the item has a price and total price (amount * price).
I want to write an expression for the total price for the group as the sum of its components.
When I build I get the error
error: Misconfigured Property: LAEItemGroup.totalPrice key path
“items.#sum.totalPrice” uses an operator as an intermediate
component
according to the documentation and the WWDC 2019 Making Apps with Core Data it should be possible to get the sum on a to many relationship.
Could someone please help me find the correct syntax or way to do so.
As a work around I tried to write a var that worked in that class as so
#objc
public var totalPrice: Double {
value(forKeyPath: "items.#sum.totalPrice") as? Double ?? 0
}
so why the KeyPath value works but not in the model editor?
I just finished a WWDC Core Data lab with Rishi who helped me with this! You should use sum:(items.totalPrice) instead of the .#sum syntax. The parentheses syntax can also be used for some other functions (e.g. count:(items) (the number of items in the to-many relationship) or max:(items.createdAt) (the date of the most recent item)).
I've now had an opportunity to check. It seems the format used by the model editor is for the aggregate operator to be at the end of the expression (which as you point out, is different from the format used in other expressions):
items.totalPrice.#sum
Use items.totalPrice.#sum as the derived property's expression in Xcode's model editor.
This only looks to work for numeric types though? I have a property maxDate with a derived property expression of
items.createdAt.#max
It compiles but throws an error at runtime:
'NSInvalidArgumentException', reason: 'currently unsupported (too many steps)
Where Date is the data type for createdAt

How to set a json object as value for a property in a Node?

I'm trying to set a property for a Node, with value as a json object. e.g., property: {jsonObject}. I cannot find any apoc procedure that could solve this problem
I tried using toJson, fromJsonMap etc apoc functions
WITH apoc.convert.fromJsonMap(jsonData) as v
MATCH (n:Node {property1: value})
SET n.property2 = v
RETURN n;
Neo.ClientError.Statement.TypeError: Property values can only be of primitive types or arrays thereof
As the error states, in neo4j a property value cannot be a map.
As cybersam says, you can't a map for a property value. However, if it's useful, you can represent them as an array - do bear in mind you'll need to manually format it as such, and retrieve.

Strange result from SQLite query using Swift

I have a table in my DB. In that table, I have a value stored (it's a date, in milliseconds): "1424386800000", in a column named "fechainicio".
I have downloaded SQLite browser in my Mac to actually check that the value is stored, and its correct, and it is.
Later, in my code, I retrieve that value using this:
var fechaInicio:Int64=Int64(results!.intForColumn("fechainicio"))
When I do this, I am receiving the value "-1542342272".
Why is the value retrieved incorrectly?
I have tried also
var fechaInicio:Int64=results!.intForColumn("fechainicio").toIntMax()
and
var fechaInicio=results!.intForColumn("fechainicio")
Isn't Int64 the correct type to retrieve a long value from a database?
The int type from (Objective-)C is a 32-bit integer type (and mapped to Int32 in Swift). This means that the
- (int)intForColumn:(NSString*)columnName;
method from FMDB can return only 32-but values. This method ultimately calls the SQLite function
int sqlite3_column_int(sqlite3_stmt*, int iCol);
which returns the lower 32-bit of the value, without any overflow check.
The proper solution is to use
- (long)longForColumn:(NSString*)columnName;
instead which returns Int64 in Swift.

Integer store value is negative

I have a Core Data model that contains an id field which is a Integer 16.
I noticed that the app was saving some object with a negative value in that field
For instance (from sqlite3 command line):
2|1|1|-32223||9|424968994|424968994|step`
I thought I'd switch to Integer 32 in order to fix it but the problem still remains. Object are still saved with a negative value in that field. I'm not quite sure why.
Was that right to switch to Integer 32? Should have this fixed the problem?
Edit: Example of value I want to save
For example the value I am storing is 33239 but it's saved as -32297
Being unsigned long compatible with Integer32 and Integer64, you should pass this type of var (unsigned long) to save in CoreData.
I have a similar issue.I am storing the 33675 integer value in CoreData but when i retrieve i got value like -26532.
In My case i defined my integer value as type int16 so range of int16 is -32,768 to 32,767 when i change my variable from int16 to int64 its resolved my issue.
for reference :Swift - Data Types

Performing valueForKeyPaths against multiple keyPaths

Let's say I have an object with some number of properties and I load up 1000s of these objects into an array. Next, I perform a series of valueForKeyPaths against these properties:
result.property1 = [array valueForKeyPath:#"#sum.property1"];
result.property2 = [array valueForKeyPath:#"#sum.property2"];
result.property3 = [array valueForKeyPath:#"#sum.property3"];
etc...
Summing these properties individually seems pretty inefficient. Is there a better way besides fast enumerating over the properties and summing them manually? i.e.
for(Foo* foo in array) {
result.property1 += foo.property1;
result.property2 += foo.property2;
result.property3 += foo.property3;
}
KVC requires keys to be strings:
A key is a string that identifies a specific property of an object. Typically, a key corresponds to the name of an accessor method or instance variable in the receiving object. Keys must use ASCII encoding, begin with a lowercase letter, and may not contain whitespace.
So the answer as far as I know is unfortunately you can't do this with valueForKeyPath: you would have to manually do it or enumerate over it.

Resources