Android部分-10:文件存储

1.说说Android中数据持久化的方式 & 使用场景。

SharedPrefence、Internal Storage、External Storage、SQLite Database、Network Connection

2.接触过MMKV吗?说说SharedPreference和它的区别。

MMKV是基于mmap内存映射的移动端key-value组件,底层序列化/反序列化使用protobuf实现。

3.第三方数据库框架用过哪些?有没有自己封装过一个SQLite的库?

4.SQLite是线程安全的吗 & SharedPreference是线程安全的吗?

5.请简单的给我说说什么是三级缓存?

网络缓存、内存缓存(LruCache)、磁盘缓存(DiskCache)

6.SharedPreference的apply和commit的区别。

apply是异步任务,无返回值

commit是同步操作,有返回值

7.谈谈你对SQLite事务的认识。

原子性、一致性、隔离性、持久性

8.千奇百怪的SQL语句考察。

9.SharedPreference跨进程使用会怎么样?如何保证跨进程使用安全?

不支持跨进程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// SharedPrefenceImpl.java
@Override
public SharedPreferences getSharedPreferences(File file, int mode) {
SharedPreferencesImpl sp;
synchronized (ContextImpl.class) {
final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
sp = cache.get(file);
if (sp == null) {
checkMode(mode);
if (getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.O) {
if (isCredentialProtectedStorage()
&& !getSystemService(UserManager.class)
.isUserUnlockingOrUnlocked(UserHandle.myUserId())) {
throw new IllegalStateException("SharedPreferences in credential encrypted "
+ "storage are not available until after user is unlocked");
}
}
sp = new SharedPreferencesImpl(file, mode);
cache.put(file, sp);
return sp;
}
}
if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
// If somebody else (some other process) changed the prefs
// file behind our back, we reload it. This has been the
// historical (if undocumented) behavior.
sp.startReloadIfChangedUnexpectedly();
}
return sp;
}

源码分析 SharedPreference类:SharedPrefence先从内存中获取实例,如果内存不存在,从磁盘中读取,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Override
public SharedPreferences getSharedPreferences(File file, int mode) {
SharedPreferencesImpl sp;
synchronized (ContextImpl.class) {
final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
sp = cache.get(file);
if (sp == null) {
checkMode(mode);
if (getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.O) {
if (isCredentialProtectedStorage()
&& !getSystemService(UserManager.class)
.isUserUnlockingOrUnlocked(UserHandle.myUserId())) {
throw new IllegalStateException("SharedPreferences in credential encrypted "
+ "storage are not available until after user is unlocked");
}
}
sp = new SharedPreferencesImpl(file, mode);
cache.put(file, sp);
return sp;
}
}
if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
// If somebody else (some other process) changed the prefs
// file behind our back, we reload it. This has been the
// historical (if undocumented) behavior.
sp.startReloadIfChangedUnexpectedly();
}
return sp;
}

10.谈谈SQLite升级要注意哪些地方?

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×