I have a properly set up NSFetchedResultsController that returns all the objects I want. However, I want to set it up so that it returns only objects that are unique by ID value, because there are several duplicate objects that have the same ID but other different properties in the DB.
I tried to set it up like the documentation says and ended up with this result:
fetchRequest.returnsDistinctResults = YES;
fetchRequest.propertiesToFetch = #[#"eventCategoryID", #"eventName", #"eventID", #"eventPeopleCount", #"eventPrice", #"eventCrewCount", #"eventStartDateTS", #"eventImageURL", #"shouldShowFriends", #"isLiked"];
However they say that you have to use this property only with NSDictionaryResultType. And moreover, it gives me a strange crash.
What are the approaches to make objects returned by NSFetchedResultsController filtered so as to have a custom unique property like ID?
So you want to find all the objects where the ID property is unique? Then yes, NSDictionaryResultType is the way to go. I think you will need two fetches, though.
First, build a fetch request to retrieve all of the ID values, and a count for each value. Use NSExpressionDescription to apply the #count function to your results, and use an NSDictionaryResultType. Group by ID. A step by step example is at http://mattconnolly.wordpress.com/2012/06/21/ios-core-data-group-by-and-count-results/.
Now you'll have a dictionary with keys for ID and the count of that ID. Filter that dictionary so that you only have IDs where the count was 1, and extract just those ID values. Now you can build a second fetch request based on ID IN ... your array of singleton IDs.
It might be possible to build a more complicated original fetch request to return only the count=1 IDs.
Related
I use sorted set data type in Redis.
I add data with command zadd. Adding data is JSON format.
How I can change value in this sorted set by score?
I need get JSON value and change one field and after update this sorted set.
I tried to add againg data with the same score, but I get dublicates
It's simple!
ZREM key data;
ZADD key score newdata;
You simply cannot UPDATE an element in SET structure. It's not possible by definition! Like I cannot EAT a cup of water, I can only DRINK it ^_^
Reply if you have any further problems.
By the way, I don't know your application need, but I have a strong feeling that SORTED SET is unsuitable for your application senario.
One does not update a set's (sorted or not) members. You'll have to remove the old member and add the new (updated JSON) in its place with the relevant score. You could wrap this in Lua or a WATCH/MULTI/EXEC block for atomicity.
You could remove first by score if you're using the list as a key (score) => value store.
ZREMRANGEBYSCORE key score score
ZADD key score data
I have this structure in index:
{ details: {errors:[], warnings:[], results:[]}}
I want a query so that I can get the count of errors warnings and results for each record.
I was looking into the script filter on an aggregate, but the thing is I am already aggregating and I'm unsure on how to use the value count filter with this since I need to
1) Get the .length attribute of the array
2) When I do that I get an error saying no attribute like that exists
Can I get a sample query? I'm guessing it will somehow incorporate the script filter.
The best way to achieve this is the token count data type told in the below link
LINK - http://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-core-types.html#token_count
You can couple this with multi fields to achieve an extra field per error , warning and results. This can be used in the query to view the count of elements.
I have a model that looks like this:
and want to display it in a UITableView using an NSFetchedResultsController. I want to list all of the Objects for a given Owner, grouped by Group. For example, if we were to view the groups for some Owner Owner A the table view might look like this:
Group A
Object A
Object B
Object C
Group B
Object A
Object D
Group C
Group D
Object C
Object E
It's also important that the Groups be ordered by their name attribute (not shown in the schema above) and that NSFetchedResultsController delegate methods get called whenever Objects are modified or added/removed from a Group.
Given those requirements, I have set up the NSFetchedResultsController to fetch a bunch of Objects with a predicate like [NSPredicate predicateWithFormat:#"ANY groups.owner = %#", someOwner], which throws an NSInvalidArgumentException exception: "to-many key not allowed here". I have tried a few other predicates, but am stuck.
Any suggestions? Is this not something I should be using an NSFetchedResultsController for? Or is there a better way to model my data?
Thanks!
Edit: I actually got the predicate working with the above code, my mistake was in my sectionNameKeyPath argument. I was trying to pass in groups.name, which was what was producing the error. I can see why that way wouldn't work, but am struggling to find a different way to achieve the desired results. Perhaps a join object?
Edit 2: This works somewhat well with a join object like this:
There are two downsides that I see now. The first is that I have to enforce uniqueness myself using code. That's easy enough, but a bit of a nuisance. The second downside, that I don't yet see a way around, is that the NSFetchedResultsController will not call its delegate for updates to Objects anymore. I can live with that downside for now, but am happy to hear better suggestions.
I think the problem you're going to see is that if you have a fetched results controller searching for Objects then it's going to find each Object only exactly once. So each object will appear exactly once in your table. Whereas what you sort of want to do is invert things and find all the Groups, then displaying all the relevant contained Objects. In terms of the fetched results controller, rather than finding rows and thereby being able to divide into sections you want to find sections and thereby figure out what to supply as rows.
The easiest thing, I think, would be to create a fetched results controller on Group and to add an intermediary object of your own that remaps those to sections and supplies group.objects (with a suitably deterministic sorting applied) as the rows per section.
If you want the rows to be dynamic then I guess the easiest thing is to create a fetched results controller per section based on the feedback of the Group controller.
If that's all getting a bit painful and you want to just write your own collection logic then you'll probably want to catch NSManagedObjectContextDidSaveNotification and rerun your logic whenever that occurs. For memory efficiency reasons you probably want to keep hold only of the objectIDs and to get the appropriate objects only when the table view requests them via the existingObjectWithID:error: method on the context. Core Data has a built-in caching mechanism that responds appropriately to memory warnings so that's all written for you.
You shouldn't need a join object to get at data in Many to Many relationships
See this answer: How to deal with many to many relationships with NSFetchedResultsController?
And example project: https://github.com/dmathewwws/Many-to-Many-CoreData
How to query the latest inserted item in with Morphia
And how to get the size of a collection?
It depends what you mean by latest, but if you have a Date field with the creation date then you would do this:
T latest = ds.find().sort("-dateCreated").get();
And this would get you the count:
int count = ds.getCount(Class.class);
When you save a document, the find method returns a object Key.
If you want to know your object with the key that morphia insert in database, just get the key and put in the object parameter. I'm doing this way in my application.
To sort can you just pass .sort("dateCreated") to sort in ASC, or .sort("-dateCreated") that sort for the DSC way.
I'm using Core Data for a tableview. My data is ordered by the distance from your current location. What I'd like is to have a new section for items with 5, 10 and 20 miles.
My distance value is stored in the data store as an NSInteger and I get it out using a NSNumber in my object model.
I've done a bit of searching around and found that I need to use the sectionNameKeyPath attribute to make the data sectioned.
My problem is that I don't know the best way to group the data. During my searches I came across either a transient property or using a category of NSNumber to work out which section the item should be in.
Are either of these methods the best way of getting my end result, if so, can anyone provide any details on how to implement it?
Thanks.
You should add a derived attribute to the object (it's not strictly needed in the model), let's name it range. I'd make it a read only property, you can cache the value or not.
When it's 0-5, return 5, 5.x - 10 return 10, etc.
Then set range as your sectionNameKeyPath.
If you want a highly customized section behavior, you need to subclass NSFetchedResultsController to give you the behavior you want. See the NSFetchedResultsController class docs for details.
You will need to subclass in this case because your not looking at a single ordered attribute like the alphabetized first letter of a string attribute but rather a range in which each attribute falls into.
This is a cleaner solution than altering the data model because you can use it to display the data many different ways in many different tableviews without having to muddy up your data model.