I have a List of objects, which I need to sort by a date property. I'm using that list to display the items in a UITableView
My problem is that, sorting a List in Realm returns a Results<myItem> instead of a List<myItem> object.
What is the proper way to sort a List in Realm without converting it into a Results object?
Workaround can be:
add extra field like timestamp of your date property (in millis), everytime you insert a record. (not required if you are sorting data based on data field)
sort query data = data!.sorted(byKeyPath: "date", ascending: false) for more options
Realm query will return RLMResult only.for unmanaged object
Related
I am trying to fetch project data from FireStore and here is my structure on firestore:
And here is my code:
FirestoreReferenceManager.rootProjects.whereField(
FirebaseKeys.ProjectDetails.inviteData,
arrayContains: [
FirebaseKeys.ProjectDetails.otherUserId: userID,
FirebaseKeys.ProjectDetails.inviteStatus: FirebaseKeys.ProjectDetails.pending
])
If I don't have isInvoiceAccess data on firestore then above code is working fine but when I add that key on firestore then above query is not giving me any results and I can not add that key because it can be true or false.
If I use below code then I am getting the results:
FirestoreReferenceManager.rootProjects.whereField(
FirebaseKeys.ProjectDetails.inviteData,
arrayContains: [
FirebaseKeys.ProjectDetails.otherUserId: userID,
FirebaseKeys.ProjectDetails.inviteStatus: FirebaseKeys.ProjectDetails.pending,
FirebaseKeys.ProjectDetails.invoiceAccess: true
])
So I want data from FireStore without adding isInvoiceAccess in my query.
The arrayContains operation only checks whether there is exact match in the array, it cannot check for partial matches.
The common workaround for your use-case is to add an additional array field for each specific values/combination of values that you want to query on. So in your case, add a field invites_uid_and_status with just those two values for each, and then use that for this query.
I've retrieved rows as objects from a class in Parse and store them in an array to use it later in a table view. I could successfully do that for all columns in the class except for two columns which are createdAt and updatedAt those two are created automatically by Parse every time we add a new row.
When I print the array, I see all the columns except createdAt and updatedAt.
This is the function I created to retrieve the data and store it in the array of objects and it works fine except for createdAt and updatedAt.
Thanks for help in advanced!
Actually I've found the answer which is that the columns objectId, createdAt and updatedAt that are created automatically by Parse are properties and we have to call them alone and they can't be retrieved with the whole object.
I called the object "article" so I can get the createdAt like this
let theDate = article.createdAt
The type here will be Date? and if you want to embed it within the table view you have to convert to String.
I have the following Entity model (name and data type) in Core Data:
Item - String
Timestamp - Date
Value - Double
Is there any way to fetch the equivalent of "select date(timestamp) as date_value, sum(value) from Entity group by date_value, value"?
Googling on groupings doesn't explain how to do this with the Date type very clearly. I tried a workaround by adding a new column called date_string where it's a formatted String version of the Timestamp column, like '2018-03-19', but it doesn't seem very elegant and doesn't really work.
You don’t do this while fetching when using Core Data but rather fetch all relevant objects and then do the grouping and summation in your code.
I would use a dictionary with dictionaries for this, date (as string) as key and double as value for the inner dictionary and the item property as key and the inner dictionary as value for the outer dictionary. Or alternatively create a summation structure and use that in a single dictionary.
I have an Entity in coredata that I fetch. I pass this Entity around to other View Controllers, one of those ViewControllers has a UITableView that I would like to display based on the Date attribute (part of the Entity I am passing around) from newest to oldest. I'm just not sure how to re-organize that data so that the date Attributes are in chronological order after the Entity has already been fetched.
Update
This is what I tried to add
CurrentStatment is my Entity, and I access another set of Entities through it called History. History has an attribute called date, which is the value I'm trying to compare.
// sort date array
historyArray = currentStatement?.history?.array as! [History]
historyArray.sorted(by:{$0.date!.compare($1.date as! Date) == .orderedAscending})
But I am getting a warning: Result to call to 'sorted(by) is unused
You can sort an array using myArray.sorted(by: ...)
So, in your case, to sort the dates newest-to-oldest, use: let sortedEntities = myEntityArray.sorted(by: {$0.Date.compare($1.Date) == .orderAscending})
More on the subject: https://developer.apple.com/reference/swift/sequence/2296242-sorted
I have a NSString as an attribute of my NSManagedObject: 'YYYY-MM-DD'. I would like to setup a NSFetchRequest that returns just a substring of the attribute.
Is this possible in IOS 7?
No. You can setup your fetch request to return an array of dictionaries where each dictionary will contain only the single key and value pair and then iterate over the array to create your list of substrings.
Alternatively, add another attribute that holds the substring and update and save it each time the main string changes.