0

I'm not really sure if this is completely save. I read some data from different online sources and save those data in a local database. I need to save a resource id from my apps resources as well, to display the data from the online source correctly localised.

Is it save to save the resource id in my database? I'm not sure if cleaning a project or a new build will make sure that the ids don't change...

Is there an alternate solution? Saving the resource name is an alternative, as long as I don't change the resource's name... Probably that's the better approach?

prom85
  • 16,896
  • 17
  • 122
  • 242
  • you may check http://stackoverflow.com/questions/24837083/does-resource-id-changes-everytime-an-application-starts. If it helps. – Anirudh Sharma Dec 09 '15 at 07:45

2 Answers2

2

Working with resource IDs is not safe. Whenever you add or delete a resource, all of the other IDs may change.

As you mentioned, naming convention is a better approach. You can get ID of a resource which given name but you should not expect much performance.

On the other hand, i would try to use another way. For example; I have a resource and an online data which pairs with it. One of datas are in online sources and other is locally hold. Why not store both of them in online sources?

Hope it helps you.

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
  • I use services like dropbox, box, drive and many more in the future and I want to display all the available media metadata in my app, localised of course... That's why I have to combine local/online sources... i anyway thought it's like that, I think the best is to make a big comment in the resources that state, that those resources are not allowed to be renamed and be careful... – prom85 Dec 09 '15 at 16:51
2

I have another solution instead of using named (strings) or integer mappings. I know, my solution is basically a tiny bit more code and its work, but the main reason I would do it differently is refactoring and changes. You want the editor to tell you if a resource is missing, so you can react. also it helps a lot, if youre not bound to filename changes etc.

as far as I understand, you need to access local images from other sources, that do not know those images. if this is correct, then it is best to use some kind of self-made image-ids. lets say you have resource aa.jpg and bb.png. That would translate to something like this:

Map<String, Integer> map
map.put("drawable_aa",R.drawable.aa);
map.put("drawable_bb",R.drawable.bb);

this allows you now to do a lookup which resource might match. You should NOT store this in a database, but build this lookup table somewhere in your application on startup (application, singleton, something you inject, however you do it). This will allow you now to have changes like refactoring, where the name of aa.jpg could change to cc.jpg, and would match to:

map.put("drawable_aa", R.drawable.cc);

and therefore not even break anything. also it allows you to have free mappings on whatever you want and just change things here. It will also complain while complaining, if something is missing.

one thing to note - make some getter function instead of accessing the map directly, that will check if the element is present. if youre returning int, then you need to have some resource set and most often its much easier code if you do not have to check everywhere if that resource is present, but it will still work - for example by using a "missing_resource.jpg" that is returned as resource, if its missing. that way its easily visible. of course you can still make something that checks and hides, but as said, then you gotta do this everytime and thats not always feasable.

hope that helps

sandrom
  • 106
  • 7
  • This is the same logic with overriding your resource id table at everytime you open your application. It just supposed to be have better performance since Map has jump to adress of object. – Emre Aktürk Dec 09 '15 at 09:02
  • http://stackoverflow.com/a/19093475/2886507 Maybe this is a bit clearer solution since not found resources returns "-1" – Emre Aktürk Dec 09 '15 at 09:02
  • If you can't control your resource names this may be a something to think about, actually I don't see a big difference in maintaining this map compared to maintaining the resource names correctly... – prom85 Dec 09 '15 at 16:54
  • @prom85 i wouldnt call it the same. it is the same goal, but what i introduced here was refactorability, which the other version does not have. in what he explained on top, this seemed like an important choice – sandrom Dec 10 '15 at 09:10