Converting between unique NSIndexPath and unique NSUInteger (or int) - iphone

Is there a better way than what is proposed here to create a one-to-one mapping between an table view cell's NSIndexPath and a unique NSUInteger or int, in order to create a unique tag property value for a widget in the cell (a UIProgressView)?
The link adds methods through a UIKit category extension that converts between NSIndexPath and int, but they only work for less than a certain number of rows.
Granted, I may not run into that limit, but I'm wondering if there is a mapping that someone has come up with that can be guaranteed to work for any number of rows.

No, there really isn't a way that is guaranteed to work for any number of rows. If you have a table with 2 sections, each with 3 billion rows, then there's no way to map those 6 billion potential NSIndexPaths into the 4 billion 32-bit NSIntegers.
(if you were building a 64-bit app, the same thing is true, but the numbers would have to be another 4 billion times bigger in the example.)
They use a limit of 10,000 rows per section in the example you linked to; if you're seriously worried you might have more rows than that, you could use a bigger constant, i.e. you could use 1,000,000 as the maximum number of rows if you know you won't have more than 4,000 sections.
Note that a "real programmer" would presumably use 65536 as the constant, of course.

If I had a need to do this I would simply shift the section path to one half of a 32-bit int, and shift the row part to the other. Yes this means you can't have more than 65535 elements in a row (in breaking it out I assumed unsigned ints since row/section will never be negative) but I'll take an assumed limit far larger than I will ever need, over a magic constant any day.
Plus it's cheaper computationally.
Usually in this situation though I set up my table view controller as a delegate for each cell, and the cell can call back the controller with any changes along with some key value used to create the cell - after all when you are populating a cell you know how to reach into your data set to pull out values. Heck, you could even simply store the IndexPath into the cell and it can call back with that. The reason I would not use the IndexPath approach or the prior one involving flattening NSIndexPath into a tag is that if you ever start changing rows dynamically the values could become unaligned between the NSndexPath you created a cell with and the value for the current dataset. Much better to have some kind of key that will always get you back to the right data logically.

Related

What kind of index should I use in postgresql for a column with 3 values

I have a table with 100Mil+ records and 237 fields.
One of the fields is a varchar 1 field with three possible values (Y,N,I)
I need to find all of the records with N.
Right now I have a b-tree index built and the query below takes about 20 min to run.
Is there another index I can use to get better performance?
SELECT * FROM tableone WHERE export_value='N';
Assuming your values are roughly equally distributed (say at least 15% of each value) and roughly equally distributed throughout the table (some physically at the beginning, some in the middle, some at the end) then no.
If you think about it you'll see why. You'll have to look up tens of millions of disk blocks in the index and then fetch them from the disk one by one. By the time you have done that, it would have been quicker to just scan the whole table and pick out the values as they match. The planner knows this and would probably not use the index at all.
However - if you only have 17 rows with "N" or they are all very recently added to the table and so physically happen to be close to each other then yes, and index can help.
If you only had a few rows with "N" you would have mentioned it, so we can ignore that one.
If however you mostly insert to this table you might find a BRIN index helpful. That can let the planner see that e.g. the first 80% of your table doesn't have any "N" blocks and so it just needs to look at the last bit.

tableau show categories from calculation even when a category is not visible

I have a calculation and it outputs multiple values. Then I am creating a table on those values. For example, in below data my formula is
if data is 1 then calculation is `one`
if data is 2 then calculation is `two`
if data is 3 then calculation is `three`
as three doesn't really appear in the output, when I create a table, three is not displayed. Is there any way to display it?
I tried table layout >> show empty rows and columns and it didn't work
data calculation
1 one
2 two
Tableau discovers the possible values for a dimension field dynamically from the query results.
If ‘three’ does not appear in your data, then how do you expect Tableau to know to make a column header for that non existent, but potential, value? It can’t read your mind.
This situation does occur often though - perhaps you want row or column headers to remain stable, even when you change filters in a way that causes some to no longer appear in the query results.
There are a few ways you can force Tableau to pad ** or **complete a domain:
one solution is to pad your data to make sure each value for your dimension field appears in at least one data row.
You can often do this easily by using a union to append some extra rows to your original data. You can often add padding rows that don’t impact any results by leaving all your Measure columns null since nulls are ignored by aggregation functions
Another common solution that is a bit more effort is to make what is known as scaffolding data source that is not much more than a list of your dimension members. You can then use that data source as a primary data source with data blending, making your original data source secondary.
There are two situations where Tableau can detect the absence of data and leave space for it in the visualization automatically
for numeric types, you can create a bin field that will automatically pad for missing bins
similarly, date fields can show missing values because, like bins, Tableau can tell when a month doesn’t appear in the data and leave room for it in the view

Is there any way to avoid PostgreSQL placing the updated row as the last row?

Well, my problem is that each time that I make an update of a row, this row goes to the last place in the table. It doesn't really matter where was placed before.
I've read in this post Postgresql: row number changes on update that rows in a relational table are not sorted. Then, why when I execute a select * from table; do I always get the same order?
Anyway, I don't want to start a discussion about that, just to know if is there any way to don't let update sentence place the row in the last place.
Edit for more info:
I don't really want to get all results at all. I have programmed 2 buttons in Java, next and previous and, being still a begginer, the only way that I had to get the next or the previous row was to use select * from table limit 1 and adding offset num++ or offset num-- depending of the button clicked. So, when I execute the update, I lose the initial order (insertion order).
Thanks.
You could make some space in your tables for updates. Change the fill factor from the default 100%, no space for updates left on a page, to something less to create space for updates.
From the manual (create table):
fillfactor (integer)
The fillfactor for a table is a percentage
between 10 and 100. 100 (complete packing) is the default. When a
smaller fillfactor is specified, INSERT operations pack table pages
only to the indicated percentage; the remaining space on each page is
reserved for updating rows on that page. This gives UPDATE a chance to
place the updated copy of a row on the same page as the original,
which is more efficient than placing it on a different page. For a
table whose entries are never updated, complete packing is the best
choice, but in heavily updated tables smaller fillfactors are
appropriate. This parameter cannot be set for TOAST tables.
But without an ORDER BY in your query, there is no guarantee that a result set will be sorted the way you expect it to be sorted. No fill factor can change that.

An Array in which 3 objects represent one Cell in uitableView

I'm loading data from sql into an NSMutableArray lets say its a table of Entries of Height and Weight each having a timeStamp (NSDate)
So the User enters the height and Weight!
I make a call to the entries to fetch all data sorted by timeStamp, that is the latest is the first.
this is the order i get,
-Height - 30 Jan 2012
-Weight - 30 Jan 2012
-Height - 30 December 2011
-Weight - 30 December 2011
-Height - Novem 2011
-Weight - Novem 2011
So i have 6 objects in my mutableArray.
ALthought in the tableView datasource, I want only 3 rows. Each with the combination of ht/wt.
So what Would be my function in cellforRow/numberOfRows/ and again retrieving these combos in didSelectRow.
2-Also, in the above Example the each row has 2 objects, What if there was another situation where i'd require 3 objects.
For example, Systolic Blood Pressure/Diastolic Blood Pressure/ Pulse pressure all entered by the user and retrieved in sorted timestamps,
IN this case each row will have 3 objects represented, and 3 rows will have 9 in total..
The number of objects represented in each row will be predefined before I call for entries, So i would know the number of objects each cell is going to have... just don't know how to go about it..
thanks for your help!
[EDIT:] I'll clear up one thing, that the Entries table - has its own Entryobject class
Each class has an-
-Value
-Timestamp
-CategoryID
The reason i don't want to have an Object defined for height/weight is because:
- height and weight are not returned in a single row.. it depends on the combination of category for example BodyMassIndex category will return a combo of 2 entries as explained above, So id have to compute here again, .. which defeats the purpose of the object itself.
Other reason being that i cannot define it, i wouldn't know if an entry is SBP, Ht or wt. Its what the CategoryObject would call, lets say for Ht entry and Wt entry, both have an CategoryID as 1. SBP, DP, PP will have a CategoryID as 2.
This way is for making the app more scalable for the future! though its taking me down!
Amm, got a solution using mAtH!! will this be super expensive on memory?
fieldCount is number of properties. ht and wt = 2!
limiterIndex=indexPath.row * fieldCount;
NSMutableArray *returnArray = [NSMutableArray new];
for(int i=limiterIndex; i<(limiterIndex+fieldCount); i++){
[returnArray addObject:[entriesArray objectAtIndex:i];
}
[cell setDataFromArray:returnArray];
[returnArray release];
Im not sure this will work, but it does seem to get the entries per cell from the all in one entries array!, at least on paper!
Seems like an expensive way to go about it?
You should make a custom object that holds every data you need in a single cell. Then put those in an array and use it as it's datasource.
In your case put the width and height data in some myObject. Then put those myObjects in an myObjectsArray and use that as its datasource.
Then in your cells extract a myObject and extract from those the height and width.
This you can use to fill the cell in cellForRowAtIndexPath:
This u can use for the dimensions of the cell in the heightForRowAtIndexpath:
Of course you can do this for other data as well. Just make a custom object which contains the data for you table.
So in short, I suggest making those custom objects and use those as the tableView's datasource.
You will figure it out.
Good luck.
I would create a model object that retrieves the master array of all entries.
This model object would also have methods that return the correct data based upon the index passed.
weightString = (NSString*)[self.dataModel getWeightAtIndex:indexPath.row];
I would then create the cells dynamically by adding text/field views to the cells as needed.
EDIT:
If you want a method that can vary in what it returns create one like :
weightString = (NSString*)[self.dataModel getDataAtIndex:indexPath.row forDataType:#"weight"];

UITableView section with maximum number of rows

I'm implementing a UITableView where the number of rows for a given section is limited to 4. For that matter, if the number of rows is less than 4, I add a row that is used as a placeholder for the next item to add (this is similar to the "Contacts" app).
I have a problem when I reach the maximum number of rows for the section. If I try to delete an object from that section, I get the following exception:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
Of course, the problem is that my numberOfRowsInSection methods returns N+1 if the number of rows is less than 4 (one extra cell for the placeholder), which triggers this inconsistency exception.
Is there anyway around this?
I've run into a similar problem because I provide a placeholder row to act as a guide in getting a user started with a table interaction in one application. The reason it is happening is that the number of rows in the section has to match - as the exception is saying.
The way to handle it is simple only call deleteRowsAtIndexPaths in cases where you are changing the number of rows in the section.
So in your case you are saying I have four rows all with content (presumably stored in an array or some other data structure). I am deleting one of these rows but I am going to be placing a placeholder row there in it's place. So the number of rows in the section for display purposes is actually not changing.
What you want to do then is delete the item from your data structure so that when cellForRowAtIndex gets called it will load your placeholder cell properly but you don't actually need to try and remove the row from the tableview.
Alternatively if you want to get the highlighting interaction I'd try out what Ian is suggesting by removing and adding the rows in one transaction - I haven't tested that out in this situation though.
I have found the user experience to be pretty smooth without the additional animations, but that depends on your app.
I think that this is expected. When you tell it to delete a row, it takes that literally -- it wants that row gone. It sounds like you just want to CLEAR the row, in which case I'd call reloadRowsAtIndexPaths: instead, making sure that your dataSource knows that this row should now be empty.
Or - and this might be better - you could call an insertRowsAtIndexPath: right after the delete. This would probably let you animate stuff a little better. Just make sure that you enclose the delete and insert statements with [tableView beginUpdates] and [tableView endUpdates] statements.
Edit: just reread the question. Nevermind my first answer, but I think my second answer should still work.