Storing integer that all mobile users can access - ios

I want to make a database for an iOS application consisting of groups that can have the same name. I am hosting my database on AWSDynamo.
Since multiple groups can have the same name, I was planning on having a groupID as the hashkey, unless someone can suggest a better method.
My main problem is storing an integer that will be the number of groups. This is so that when a user creates a new group the number will be incremented and the new group will get that number as its groupID.
How can I store an integer in such a fashion that all users can access it from the app?

UUID – Universally unique identifier
You can use a UUID (String) as your groupID in your groups table, and use conditional writes (PutItem, UpdateItem) to handle the extremely rare case where there is a collision. If you create a UUID for a new group where the UUID already is assigned to another group, you will get a ConditionalCheckFailedException so you can retry with a new UUID. You don't need to use an incrementing sequence to uniquely identify groups.

Related

How to count cases with the same ID but different variables in SPSS

I have a data set which has 4420 attendances to a medical department from 1120 people. Each person has a unique ID number and other columns are demographics and primary care provider. I want to filter the data so I can work out how many times each person attends the department and then analyse the data by demographics eg primary care provider or age. It shows whether each attendance is primary or duplicate but I can't figure out how to work out attendances per person.
If what you want to do is to count the number of times each person has visited (assuming each one is represented by a single row in the data), use the AGGREGATE command breaking on the ID variable to add the number of instances to the file as a new variable. In the menus, Data>Aggregate, move the ID variable into the box for Break Variable(s), check the box for Number of cases under Aggregated Variables, change the default N_BREAK to another name if you want, and click OK. That will add a new variable to the data with the number of instances for each unique ID.

Prevent passbooks group together

My passbooks are user's store cards, they have same certificate (same pass type identification) and category as discuss in this link, but I don't want them to group together since they are for many different stores or business. Any way to do that.
For storecard passes, the only way to prevent grouping is to use a different certificate. coupon, storecard and generic passes with the same passTypeIdentifier will always be grouped together.
For eventTicket and boardingPass types, you can use a groupingIdentifier key to separate passes with the same passTypeIdentifier into different groups.
groupingIdentifier |
string |
Optional for event tickets and boarding passes; otherwise not allowed. Identifier used to group related passes. If a grouping identifier is specified, passes with the same style, pass type identifier, and grouping identifier are displayed as a group. Otherwise, passes are grouped automatically.
Use this to group passes that are tightly related, such as the boarding passes for different connections of the same trip.
Available in iOS 7.0.
PassKit Package Format Reference

Rails model.create set id

I wonder if it's possible to to run Model.create() such that instead of taking next free id integer it takes the lowest free integer.
For example, assume we have records for id=10..20 and we don't have records for id=0..9, I want create instance of Model with id starting from 0 (in normal Mode.create() in would create instance staring from 21)
Preferably I want to do it in automatic manner. I don't want to change id by explicitly defining it.
DB
You'll be best doing this at database-level (look at altering the auto-increment number)
Although I think you can do this in Rails, I would highly recommend using the DB functionality to make it happen. You can do something like this in PHPMYAdmin (for MYSQL):
If you set the Auto-Increment to the number you wish to start at, every time you save data into the DB, it will just save with that number. I think using any Rails-based method will just overcomplicate things unnecessarily.
I'd discourage it.
Those ids serve solely as unique identifiers for rows in a table, and it's the database's job to assign one. You can verify that the model doesn't require an id to be saved:
m = Model.new
# populate m with data
m.name = "Name"
# look at what m contains
m
# and save it
m.save
# now inspect it again and see it got its unique id
m
While it might be possible to modify ids, it's not a good practice to give more sense to ids — when each new record gets a unique id at any time it's easier to debug possible DB structure errors that might occur during development. Like, say, some associated objects suddenly show up in a new user's account. Weird enough, right? That can happen and, worst case, can show up in production resulting in a severe security breach.
Keeping ids unique at all times eliminates this bug's effect. That seems much more important if the associated objects store confidential information and you care about keeping them safe. Encryption concerns aside.
So, to be sure in every situation, developers have adopted a practice of not giving id any other role other than uniquely identifying a row in a table. If you want it to do something else, consider making another field for that purpose.

How can safely assign an incrementing ID to a subset of objects?

I have an Order object which can be in an unpaid or paid state. When an order is paid, I want to set an order_number which should be an incrementing number.
Sounds easy enough, but I'm worried about collisions. I can imagine one order having an order_number stored in memory, about to save and then another order saves itself, using that number, now the one in memory should be recalculated, but how?
You can create a database table that just contains an AUTO_INCREMENT primary key. When you need to to get a new order_number just do an insert into this table and get the the value of the primary key for the created row.
There are a lot of approaches. Essentially you need a lock to ensure that each request to the counter always return a different value.
Memcache, Redis and some key-value stores have this kind of counter feature. eg, each time you want to get a new order_number, just call the incr command of Redis, it will increment the counter and return the new value.
A more complex solution can be implemented via the trigger/stored procedure/sequence features of RMDBS(like mysql). For mysql, create a new table contains an AUTO_INCREMENT primary key. When you want to get a new order_number, make an insert into this table and get last_insert_id(). If you want ACID, just wrap the procedure in a transaction.

Can we use the ids again for whom the record has been deleted?

I was doing RoR tutorial wherein we could add,update,delete user details in the application and simultaneously an id gets auto defined with user,but once we delete a user details then for that id it displays record not found.
Can we use that id again?
From your comments it looks like you're trying to save on using high-value IDs by re-using lower value IDs after they've been freed. In general this is not considered a good idea.
The likelihood that you will run out of IDs at the top end is minimal (zero if you keep making your ID column accept larger integers) however reassigning IDs has the potential to open you up to problems. If, for instance you wanted to delete a user but keep content they had created (e.g. blog posts) then reassigning the IDs would mean that the new ID owner becomes the owner of those old comments.
It feels wasteful but the best thing to do is just leave old, vacant IDs vacant and eat up new ones.
You can use something like this (Rails 3 syntax)
#user = User.find_by_id(params[:id]) || User.where("id > ", params[:id]).first || User.where("id < ", params[:id]).last
I do not recommend re-using unique ID's.
First, ask yourself why do you assign unique ID's to users:
To uniquely identify users
To create unique URLs to a user's resources
To link other objects (orders, blog posts, game data) to that specific user
So, if you've deleted a user - what happens?
In almost every app I've written a user always leaves traces. Either from URLs that were once exposed to the internet (think indexed by google) or data from that user that's kept as records (orders, etc).
Reusing a user ID would cause problems - and thus work to refactor the application to cope with those problems. In 99% of these cases the easiest solution it to just keep generating new, unique IDs for those users.
There are also situations that you need to keep data from a deleted user around (e.g. financial systems and webshop are good examples). This would keep the unique ID alive after the user is deleted - you can't reuse it.
TL;DR: Reusing unique IDs is possible, but poses problems. Easiest solution around those problems is generating new unique IDs.
As an added note, unique IDs don't have to be auto incremented integers.

Resources