Please tell about namespaces

I am using TinyDB on my application and there's also a property called 'namespace'

However, I am not able to understand what the property means as in the documentation it just says:

image

Please help me with what are namespace. Also, don't provide me with fruitstand example, just tell me briefly what it really means.

Thanks,
Krish

You can have one TinyDB, containing more than one namespace, to logically arrange stored values. See an example using a Multi-Namespace TinyDB by @ChrisWard

https://www.professorcad.co.uk/appinventorsnippets#TinyDBNamespace

1 Like

If you have more than one screen and you want every screen or some screens use the data from the others, you can set the namespace to the same. Every TinyDB that haves the same namespace acess the same data. A TinyDB which haves a different namespace than others have different data.

1 Like

TinyDB NameSpaces are separate .xml files useful to separate different data values that might have the same tags otherwise.

Here is a sample project ...

1 Like

In this example TinyDB1, TinyDB2 and TinyDB3 are the same database, because it has the same namespace (TinyDB1)

TinyDB4 is a different database than the previous ones, because it has a different namespace (TinyDB4).

3 Likes

Thanks to everyone. I totally got it now.

So I think we can basically say that tinyDB's having the same namespaces sync to each other to store totally the same data under their respective tag.

Although I have some questions:-

  1. Can one TinyDB have multiple namespaces?
    a) If the answer Is Yes, why do we need to create different/multiple TinyDB's?
    b) If the answer is No, why do we need to have namespaces if we can store all the data in just
    one TinyDB rather than syncing data from multiple DB's?

Please clear my confusion and also a suggestion to keep this discussion forever visible to everyone as this concept gets really confusing to newbies.

Can one TinyDB have multiple namespaces?

Yes

a) If the answer Is Yes, why do we need to create different/multiple TinyDB's?

You don't have to, on the same screen, but if using more than one screen and TinyDB, you will need the component on the other screens.

1 Like

Sorry, but I didn't quite get that.

I think we can surely get data on different screens from same tinyDB.

Right ?

Which is what I said. It is about being able to drag out another component. Don't over think this.

TinyDB is the same database, no matter how many tinydb components you drag out on how many screens.

If you set different namespaces, then you will have different storage containers. You must remember to set the namespace, every time, before trying to get at its contents!

1 Like

Maybe it would make sense to get a bit of history to understand how it works. In earlier versions of App Inventor, TinyDB only ever wrote to the same backing file (TinyDB1.xml). This was useful for persisting data across app restarts and passing data between screens, since every instance of TinyDB in the app would read/write the same backing store.

However, we also observed that many people were surprised by the fact that if they create multiple TinyDBs on the same screen that data would randomly "appear" in one TinyDB written to another. Conceptually people expected these different TinyDBs to be backed by different data stores and not share the same data.

We introduced the Namespace property to address the confusion around having multiple TinyDB instances on the same screen. While they still initialize Namespace to "TinyDB1" to simulate the old behavior, you can change the Namespace property to store data in a new file (<namespace>.xml). For example, if you change Namespace to be "People", then you'll end up with a "People.xml" file. and so forth. For two TinyDB (same screen or different) to manipulate the same data, they need to have the same Namespace property. You can programmatically set the Namespace, so even if you have one instance of the TinyDB component it can write to many different backing files. Or you can have many TinyDB each with a fixed Namespace property.

3 Likes

(added to FAQ)

1 Like

Thank you very much. MIT app inventor community is so far the best and highly active community that I have seen.

A great thanks to everyone​:pray::pray:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.