Using NSFetchedResultController with no sorting - cocoa-touch

1) I wanna display my search results in the same order returned by the web Service, but it seems 'An instance of NSFetchedResultsController requires a fetch request with sort descriptors'.
2) I still wanna use a NSFetchedResultsController because I allow user to sort by date, etc, but if no sorting is chosen I want to display them in the exact order I got them.
3) Another thing, depending on the search, the items might have different priority. Since I store every item, I cannot just create a priority for each since it won't apply to every case.
Thanks in advance

Lucas,
If you want to enforce an order, then you need an attribute to sort against. I suggest you add a serial number to your model and bump it as you insert items.
Andrew

Related

How to get distinct xp values?

Each product has two xp parameters - productLine, productType.
Under productLine, there are multiple product types.
I need to fetch list of distinct productType under each productLine.
There is limit on listing all products due to pagination.
Is it possible with ordercloud?
The short answer is there's no shortcut way to do this. Typically in these scenarios it works the other way - you have a pre-defined list of possible values and restrict what goes in via a dropdown list in the UI, or an enum in the integration layer, or something along those lines. For your scenario, you would need to resort to fetching all products (page by page) and keeping track of those unique values. Ideally that one be a one-time thing and going forward I'd suggest validating/restricting the input, although I don't know your exact requirements.

PowerBI - Trying to sort a one-to-many column by the many column in visualizations where it would always be one-to-one

I'm working with bus data where each record in the raw data records a bus reaching a stop and how many people got on or off the bus. The raw data also includes which route the bus is for every record, and by creating an ID of [bus route] + [bus stop], I can then reference a manually maintained stop order table so that the order for the stop in the context of the route is available for sorting. E.g order for stop100.route5 = 4; order for stop100.route6 = 8 , etc.
Example of the same stop having a different order here:
The separate table for stop order I mentioned is set up like this (filtered to show different values for the same stop):
Now that I'm trying out PowerBI I'm hitting a bit of a roadblock. I can't sort the stop column by the stop order column, as there are multiple values for each stop depending on the route in question. I know that I can still use stop order and stop as row values and toggle the 'expand all' setting, but my ideal is to hide the stop order numbers, plus in drill-down situations to stop level, the stops will be sorted alphabetically rather than by order number.
For any experts I have a few avenues I thought might be viable workarounds with enough know-how:
Is there a way to hide portions of a field's values in visuals? This whole thing wouldn't be an issue if I could use the stop.route IDs in place of the stop name field, but I would want to hide the .route portion of the value.
Is there any long-winded way to create a one-to-one sorting that I can use to sort the stop column? Some sort of dynamic calculation that filters a one-to-many to a one-to-one, as every context I plan on using this, there will be only one possible order # for the stop.
Many thank yous to anyone with advice!

Elastic Search, Nest. functional sorting

I'm building a filter page, with facets etc, which works as it should.
Now the our customer has a request to, basically "Be able to decide which sorting the items comes out in".
Each product is decorated with a Product Display Order, and is in a Product Line.
We got these example Product Display Orders:
1. Featured Item
2. Core Item
3. Spare Part
4. Utility
And these Product Lines:
1. Hammers
2. Saw
3. Wood
and the sorting is like this:
Sorting should firstly be based on Product Display Orders, secondly by product lines, thirdly Alphabetically.
So all products which is a Featured Item is listed first, and all these Featured Items is then sorted by their product line, and if some product are in the same Featured Item and Product Line, then its alphabetically.
The challenge is: I can't just get the sorting of Product Display order items and product lines as a number on the product, i only got a name/id.
We've thought of Boosting based on if the product are in the different categories, but it seems a bit messy.
OR
See if it possible to have some logic in the Sorting.
Sort by productDisplayOrder:
1. featured, 2. core Item ...
Then by ProductLines:
1. Hammers, 2. Saw ...
Then by Name DESC.
Which way is the best way to have this sorting, is it possible to give this logic to elastic, if it is a match and then sort it. Or are we needed to twist the boosts of product?
Hopefully this makes sense for you.
Thanks in advance! :)
Option 1). Quickest/Best performing solution would be to create new/separate integer fields for productDisplayOrder and ProductLine and then use those in your sort criteria as described (after reindexing and validating the the data is indexed as expected).
Option 2) If you want more nuance than described (eg higher scoring matches can 'break through' the ordering ceiling described) then you can explore using a Function Score Query to implement a custom scoring strategy that takes productDisplayOrder and ProductLine into consideration in generating an overall match score.
Option 3). If you can't change the mapping and reindexing your data, you can use Script-Based Sorting to generate sorting values from the currently indexed productDisplayOrder/ProductLine text using a script (eg Groovy). Keep in mind that query performance will be worse than the first two options.

Redis: paginated ordered zrank fetch of a member in multiple sorted sets

New to Redis. Need some help.
USE CASE:
I have thousands of leaderboards. They have usernames in them with appropriate scores. A user can belong to 1 or more leaderboards. I need an efficient way to get the rank of every leaderboard a particular user belongs to, preferrably sorted by rank and with pagination. Typical user will belong to hundreds of leaderboards.
AS FAR AS I GOT:
I keep a set for each user containing boards he belongs to. To get a user's ranks I get his set of boards then zrank each board in the set and then order it by rank in my code. This seems very inefficient and does not support pagination.
I have been reading and brainstorming and I am stuck. What I need is something like:
user1:boards (a,c,e)
board:a (user1,user23,user5)
board:b (user2,user7,user12)
board:c (user2,user1,user42)
board:d (user36,user4,user9)
board:e (user6,user19,user1)
SORT user1:boards BY board:*->user1
Similar to sorting by hash fields except -> in this case means the sorted set score of the member provided. Would there be any performance improvement if such a feature existed? Or would it be the same as pipelining all the zranks?
Thanks.
To make your reads efficient, you just have to make a minor change to your writes.
Currently you are storing user boards in a set, store them in a sorted set instead. Lets call it user_boards_sorted_set.
So whenever you increase the score of a user 1 in a leaderboard sorted set (board1 for example) , you run a zrank for user 1 on board1, and that rank becomes score for the user1 in user_boards_sorted_set.
This way user_boards_sorted_set always contains all the boards the user belongs to, and the scores against each entry contain his rank in that particular leaderboard.Run a ZRANGE on user_boards_sorted_set and you will have the user and his ranks in all the leaderboards sorted by rank.
UPDATE : Based on feedbacks in the comments, and an incorrect assumption made in the above answer .
Another good way would be to use Lua scripting to get individual board rankings by doing ZRANK on all the boards that the user belongs to, and sorting it in LUA itself. This will give significant performance gain as all the ZRANKS and then sorting are done on server side itself, and reduces network transfers.

What is the best way to fake a SQL array or list?

I'm building a chatroom application, and I want to keep track of which users are currently in the chatroom. However, I can't just store this array of users (or maybe a list would be better) in a field in one of my records in the Chatroom table.
Obviously one of the SQL data types is not an array, which leads me to this issue: what is the best way to fake/mock array functionality in a SQL database?
It seems there are 3 options:
1: Store the list/array of users as a string separated by commas, and just do some parsing when I want to get it back to an array
2: Since the max amount of users is allowed to be 10, just have 10 extra fields on each Chatroom record representing the users who are currently there
3: Have a new table Userchats, which has two fields, a reference to the chatroom, and a user name
I dunno, which is the best? I'm also open to other options. I'm also using Rails, which seems irrelevant here, but may be of interest.
Option 3 is the best. This is how you do it, in a relational schema. It is also the most flexible and future-proof option.
It can grow easier in width (extra columns say, a date joined, a channel status, a timestamp last talked) and length (extra rows when you decide there now can be 15 users in a room instead of 10).
The proper way to do this is to add an extra table representing an instance of a user being in a chatroom. In most cases, this is probably what you will want to do, since it gives you more flexibility in the types of queries you can do (for instance: list all chatrooms a particular user is in, find the average number of people in each chatroom, etc.) You would just need to add a new table - something like chat_room_users, with a chat_room_id, and a user_id.
If you're deadset on not adding an extra table, then Rails (or more specifically ActiveRecord), does have some functionality to store data structures like arrays in a SQL column. Just set up your column as a string or text type in a Rails migration, and add:
serialize :users
You can then use this column as a normal Ruby array / object, and ActiveRecord will automatically serialize / deserialize this object as you work with it. Keep in mind that's there are a lot of tradeoffs with this approach - you will never be able to query what users are in a particular room using SQL and will instead need to pull all data down to Ruby before working with it.