0
# NameSpace Management
1
2
NameSpace provides a facade for managing MMKV instances within custom root directories, enabling isolated storage environments for different application components or use cases.
3
4
## Capabilities
5
6
### NameSpace Creation
7
8
Create and manage NameSpace instances with custom root directories.
9
10
```java { .api }
11
/**
12
* Create a NameSpace with custom root directory
13
* @param dir the customize root directory of a NameSpace
14
* @return a NameSpace with custom root dir
15
* @throws RuntimeException if there's an runtime error
16
*/
17
static NameSpace nameSpace(String dir);
18
19
/**
20
* Get the default NameSpace identical with the original MMKV with the global root dir
21
* @throws RuntimeException if there's an runtime error
22
*/
23
static NameSpace defaultNameSpace();
24
```
25
26
**Usage Examples:**
27
28
```java
29
// Create custom NameSpace for user-specific data
30
NameSpace userSpace = MMKV.nameSpace("/data/user_storage");
31
String rootDir = userSpace.getRootDir();
32
33
// Get default NameSpace
34
NameSpace defaultNS = MMKV.defaultNameSpace();
35
```
36
37
### NameSpace Instance Management
38
39
Create MMKV instances within the NameSpace's root directory.
40
41
```java { .api }
42
/**
43
* Create an MMKV instance with an unique ID (in single-process mode)
44
* @param mmapID The unique ID of the MMKV instance
45
* @throws RuntimeException if there's an runtime error
46
*/
47
MMKV mmkvWithID(String mmapID);
48
49
/**
50
* Create an MMKV instance in single-process or multi-process mode
51
* @param mmapID The unique ID of the MMKV instance
52
* @param mode The process mode of the MMKV instance
53
* @throws RuntimeException if there's an runtime error
54
*/
55
MMKV mmkvWithID(String mmapID, int mode);
56
57
/**
58
* Create an MMKV instance with expected capacity
59
* @param mmapID The unique ID of the MMKV instance
60
* @param mode The process mode of the MMKV instance
61
* @param expectedCapacity The file size you expected when opening or creating file
62
* @throws RuntimeException if there's an runtime error
63
*/
64
MMKV mmkvWithID(String mmapID, int mode, long expectedCapacity);
65
66
/**
67
* Create an MMKV instance with encryption key
68
* @param mmapID The unique ID of the MMKV instance
69
* @param mode The process mode of the MMKV instance
70
* @param cryptKey The encryption key (no more than 16 bytes)
71
* @throws RuntimeException if there's an runtime error
72
*/
73
MMKV mmkvWithID(String mmapID, int mode, String cryptKey);
74
75
/**
76
* Create an MMKV instance with all customize settings
77
* @param mmapID The unique ID of the MMKV instance
78
* @param mode The process mode of the MMKV instance
79
* @param cryptKey The encryption key (no more than 16 bytes)
80
* @param expectedCapacity The file size you expected when opening or creating file
81
* @throws RuntimeException if there's an runtime error
82
*/
83
MMKV mmkvWithID(String mmapID, int mode, String cryptKey, long expectedCapacity);
84
```
85
86
### NameSpace File Operations
87
88
Manage MMKV storage files within the NameSpace.
89
90
```java { .api }
91
/**
92
* Check whether the MMKV file is valid or not
93
* Note: Don't use this to check the existence of the instance
94
* @param mmapID The unique ID of the MMKV instance
95
*/
96
boolean isFileValid(String mmapID);
97
98
/**
99
* Remove the storage of the MMKV, including the data file & meta file (.crc)
100
* Note: the existing instance (if any) will be closed & destroyed
101
* @param mmapID The unique ID of the MMKV instance
102
*/
103
boolean removeStorage(String mmapID);
104
105
/**
106
* Check existence of the MMKV file
107
* @param mmapID The unique ID of the MMKV instance
108
*/
109
boolean checkExist(String mmapID);
110
```
111
112
### NameSpace Backup and Restore
113
114
Backup and restore operations within the NameSpace directory.
115
116
```java { .api }
117
/**
118
* Backup one MMKV instance to destination directory
119
* @param mmapID the MMKV ID to backup
120
* @param dstDir the backup destination directory
121
*/
122
boolean backupOneToDirectory(String mmapID, String dstDir);
123
124
/**
125
* Restore one MMKV instance from source directory
126
* @param mmapID the MMKV ID to restore
127
* @param srcDir the restore source directory
128
*/
129
boolean restoreOneMMKVFromDirectory(String mmapID, String srcDir);
130
```
131
132
## Types
133
134
```java { .api }
135
class NameSpace {
136
/**
137
* Get the root folder of this NameSpace
138
* @return The root folder path
139
*/
140
String getRootDir();
141
}
142
```