[英]Does every object contained within a serialized object have to be serialized as well? (Android, Java)
I am working on Android Studio (Java), and have incorporated Google Firebase into my project.我正在使用 Android Studio (Java),并将 Google Firebase 合并到我的项目中。 In my main activity, I instantiate a firebase user, and store all the relevant information in a custom "User" class.
在我的主要活动中,我实例化了一个 firebase 用户,并将所有相关信息存储在自定义“用户”class 中。
public class User implements Serializable{
FirebaseUser user;
FirebaseAuth mAuth;
String uid;
public User()
{
System.out.println("Creating User");
mAuth = FirebaseAuth.getInstance();
user = mAuth.getCurrentUser();
uid = user.getUid();
}
}
I have a multi tab setup, where each tab is a fragment.我有一个多选项卡设置,其中每个选项卡都是一个片段。 Each fragment needs access to the lone user object representing the currently signed in user, so I pass the User object to each fragment.
每个片段都需要访问代表当前登录用户的单独用户 object,因此我将用户 object 传递给每个片段。 In order to do that, it needs to be serialized, which it is.
为了做到这一点,它需要被序列化,它是。 Here is one of the fragments:
这是其中一个片段:
public class tab_list extends Fragment {
private static final String ARG_PARAM1 = "user_List";
private User user;
public tab_list() {
// Required empty public constructor
}
public static tab_list newInstance(User u) {
tab_list fragment = new tab_list(); // This calls the empty constructor above
Bundle args = new Bundle();
args.putSerializable(ARG_PARAM1, u);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
user = (User) getArguments().getSerializable(ARG_PARAM1);
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState){
//Do Stuff
}
}
Everything works great, The tab has access to the data stored in user, and I can do everything I need too.一切都很好,选项卡可以访问存储在用户中的数据,我也可以做我需要的一切。 until the app ends up in the background, If my phone puts the app into the background, either by switching apps or pulling down the tray or letting the screen sleep: the app crashes and I get this error:
直到应用程序结束在后台,如果我的手机通过切换应用程序或拉下托盘或让屏幕休眠将应用程序置于后台:应用程序崩溃并且我收到此错误:
"Parcelable encountered IOException writing serializable object". “Parcelable 遇到 IOException 写入可序列化对象”。 .
. .
. Caused by: java.io.NotSerializableException: com.google.firebase.auth.internal.zzn
引起:java.io.NotSerializableException:com.google.firebase.auth.internal.zzn
To me, this implies that the parts of firebase auth ALSO need to be serialized.对我来说,这意味着 firebase auth 的部分也需要序列化。 Is this the correct interpretation?
这是正确的解释吗? I feel like this is going to be a long rabbit hole of serialization if that is the case, if it is even possible to serialize classes I don't control (and don't want to alter).
如果是这样的话,我觉得这将是一个很长的序列化兔子洞,如果它甚至有可能序列化我不控制(并且不想改变)的类。 Is there a way around this?
有没有解决的办法? What is the proper way to do this?
这样做的正确方法是什么?
Thank you for your time.感谢您的时间。
To me, this implies that the parts of firebase auth ALSO need to be serialized.
对我来说,这意味着 firebase auth 的部分也需要序列化。 Is this the correct interpretation?
这是正确的解释吗?
Yes, if you want to serialize something with Java's built-in serialization, it will attempt to serialize all nested objects.是的,如果您想使用 Java 的内置序列化来序列化某些东西,它会尝试序列化所有嵌套对象。 As you're discovering, that's not really so easy with Firebase objects.
正如您所发现的那样,使用 Firebase 个对象并不是那么容易。 They are simply not meant to be serialized this way.
它们根本不应该以这种方式序列化。
Your data objects to be serialized should ideally contain only the most basic, raw data.要序列化的数据对象最好只包含最基本的原始数据。 They should not contain more complex objects (especially those that are not themselves implementing
Serializable
).它们不应包含更复杂的对象(尤其是那些本身未实现
Serializable
的对象)。 There is really no need to serialize FirebaseAuth, as that's a singleton. And you for FirebaseUser, you should consider pulling out only the relevant data from that object.确实没有必要序列化 FirebaseAuth,因为它是一个 singleton。对于 FirebaseUser,您应该考虑仅从该 object 中提取相关数据。
Fields declared as transient
are not serialized by the default serialization mechanism.默认序列化机制不序列化声明为
transient
的字段。
Language spec, 8.3.1.3 语言规范,8.3.1.3
I would suppose you can therefore make the FirebaseUser and FirebaseAuth members transient, while 'manually' extracting any parts of them you need.因此,我想您可以使 FirebaseUser 和 FirebaseAuth 成员成为临时成员,同时“手动”提取您需要的任何部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.