unity+firestore(v7.0.1)でChangeTypeを使うとandroidでcrushする

こんにちは、もぐめっとです。

来週リリースを控えていてかなり余裕がないです。


今回は表題の通りなのですが、unity+firestore(v7.0.1)でドキュメントやコレクションをListenしているときに、ドキュメントがAddされたか、Modifyされたか、Deleteされたかなどを表すChangeTypeにアクセスするとandroidでcrushします。

こんな感じでクラッシュします。

```
--------- beginning of crash
2020-12-09 19:42:07.756 16360-16428/? E/AndroidRuntime: FATAL EXCEPTION: UnityMain
    Process: sk.inlogic.chessstrategyboardgame, PID: 16360
    java.lang.Error: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    Version '2019.4.16f1 (e05b6e02d63e)', Build type 'Release', Scripting Backend 'mono', CPU 'armeabi-v7a'
    Build fingerprint: 'Sony/H4113/H4113:9/50.2.A.3.77/2122144346:user/release-keys'
    Revision: '0'
    ABI: 'arm'
    Timestamp: 2020-12-09 19:42:06+0100
    pid: 16360, tid: 16428, name: UnityMain  >>> sk.inlogic.chessstrategyboardgame <<<
    uid: 10345
    signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
        r0  00000000  r1  0000402c  r2  00000006  r3  00000008
        r4  00003fe8  r5  0000402c  r6  c524dcf4  r7  0000010c
        r8  0000000b  r9  00000000  r10 e58e928e  r11 c524dd68
        ip  e92cf3cc  sp  c524dce0  lr  e923a115  pc  e9230fde
    
    backtrace:
          #00 pc 0001cfde  /system/lib/libc.so (abort+58) (BuildId: 09d6997dde9d629deac5acecc91661ea)
          #01 pc 0034e351  /system/lib/libart.so (art::Runtime::Abort(char const*)+1664) (BuildId: fb1afbd3fdff41a542b7e911dc4597c1)
          #02 pc 0000738f  /system/lib/libbase.so (android::base::LogMessage::~LogMessage()+494) (BuildId: 414414495a0b4b17c57c98a57c1d3786)
          #03 pc 002324cf  /system/lib/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+1214) (BuildId: fb1afbd3fdff41a542b7e911dc4597c1)
          #04 pc 0026a599  /system/lib/libart.so (art::JNI::CallObjectMethodV(_JNIEnv*, _jobject*, _jmethodID*, std::__va_list)+1020) (BuildId: fb1afbd3fdff41a542b7e911dc4597c1)
          #05 pc 0015482d  /data/app/sk.inlogic.chessstrategyboardgame-POoH5lCOcJtTGLOLxpYLYA==/lib/arm/libFirebaseCppApp-7_0_1.so (_JNIEnv::CallObjectMethod(_jobject*, _jmethodID*, ...)+16) (BuildId: 634d4e75feefbd66e47541a864ff04b9)
          #06 pc 001835df  /data/app/sk.inlogic.chessstrategyboardgame-POoH5lCOcJtTGLOLxpYLYA==/lib/arm/libFirebaseCppApp-7_0_1.so (std::__ndk1::enable_if<!(is_same<firebase::firestore::DocumentChangeTypeInternal, void>::value), firebase::firestore::jni::ResultTypeMap<firebase::firestore::DocumentChangeTypeInternal, IsPrimitive<firebase::firestore::jni::JniTypeMap<std::__ndk1::decay<firebase::firestore::DocumentChangeTypeInternal>::type>::type>::value>::type>::type firebase::firestore::jni::Env::CallHelper<firebase::firestore::DocumentChangeTypeInternal, _jobject* (_JNIEnv::*&)(_jobject*, _jmethodID*, ...), _jobject*, _jmethodID*>(_jobject* (_JNIEnv::*&&&)(_jobject*, _jmethodID*, ...), _jobject*&&, _jmethodID*&&)+62) (BuildId: 634d4e75feefbd66e47541a864ff04b9)
          #07 pc 0018348f  /data/app/sk.inlogic.chessstrategyboardgame-POoH5lCOcJtTGLOLxpYLYA==/lib/arm/libFirebaseCppApp-7_0_1.so (firebase::firestore::jni::ResultTypeMap<firebase::firestore::DocumentChangeTypeInternal, IsPrimitive<firebase::firestore::jni::JniTypeMap<std::__ndk1::decay<firebase::firestore::DocumentChangeTypeInternal>::type>::type>::value>::type firebase::firestore::jni::Env::Call<firebase::firestore::DocumentChangeTypeInternal>(firebase::firestore::jni::Object const&, firebase::firestore::jni::Method<firebase::firestore::DocumentChangeTypeInternal> const&)+46) (BuildId: 634d4e75feefbd66e47541a864ff04b9)
          #08 pc 0018343b  /data/app/sk.inlogic.chessstrategyboardgame-POoH5lCOcJtTGLOLxpYLYA==/lib/arm/libFirebaseCppApp-7_0_1.so (firebase::firestore::DocumentChangeInternal::type() const+30) (BuildId: 634d4e75feefbd66e47541a864ff04b9)
          #09 pc 0000a516  <anonymous:c41a7000>
    
    managed backtrace:
          #00 (wrapper managed-to-native) Firebase.Firestore.FirestoreCppPINVOKE:DocumentChangeProxy_type (System.Runtime.InteropServices.HandleRef)
          #01 Firebase.Firestore.DocumentChangeProxy:type () <0x2b>
          #02 Firebase.Firestore.DocumentChange:get_ChangeType () <0x23>
          #03 CorrespondenceMode:LoadMatch (Firebase.Firestore.QuerySnapshot) <0x7b>
          #04 Firebase.Firestore.Query/<Listen>c__AnonStorey0/<Listen>c__AnonStorey1:<>m__0 () <0x63>
          #05 Firebase.Dispatcher:Run<TResult_REF> (System.Func`1<TResult_REF>) <0x63>
2020-12-09 19:42:07.758 16360-16428/? E/AndroidRuntime:       #06 Firebase.Platform.FirebaseHandler:RunOnMainThread<TResult_REF> (System.Func`1<TResult_REF>) <0x53>
          #07 Firebase.Firestore.Query/<Listen>c__AnonStorey0:<>m__0 (Firebase.Firestore.QuerySnapshotProxy,Firebase.Firestore.FirestoreError,string) <0xe7>
          #08 Firebase.Firestore.Query:QuerySnapshotsHandler (int,intptr,Firebase.Firestore.FirestoreError,string) <0x8b>
          #09 (wrapper native-to-managed) Firebase.Firestore.Query:QuerySnapshotsHandler (int,intptr,Firebase.Firestore.FirestoreError,intptr)
          #10 (wrapper managed-to-native) Firebase.AppUtilPINVOKE:PollCallbacks ()
          #11 Firebase.AppUtil:PollCallbacks () <0x7>
          #12 Firebase.Platform.FirebaseAppUtils:PollCallbacks () <0xf>
          #13 Firebase.Platform.FirebaseHandler:Update () <0x47>
          #14 Firebase.Platform.FirebaseMonoBehaviour:Update () <0x57>
          #15 (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    
        at libc.abort(abort:58)
        at libart.art::Runtime::Abort(char const*)(Abort:1664)
        at libbase.android::base::LogMessage::~LogMessage()(~LogMessage:494)
        at libart.art::JavaVMExt::JniAbort(char const*, char const*)(JniAbort:1214)
        at libart.art::JNI::CallObjectMethodV(_JNIEnv*, _jobject*, _jmethodID*, std::__va_list)(CallObjectMethodV:1020)
        at libFirebaseCppApp-7_0_1._JNIEnv::CallObjectMethod(_jobject*, _jmethodID*, ...)(CallObjectMethod:16)
        at libFirebaseCppApp-7_0_1.std::__ndk1::enable_if<!(is_same<firebase::firestore::DocumentChangeTypeInternal, void>::value), firebase::firestore::jni::ResultTypeMap<firebase::firestore::DocumentChangeTypeInternal, IsPrimitive<firebase::firestore::jni::JniTypeMap<std::__ndk1::decay<firebase::firestore::DocumentChangeTypeInternal>::type>::type>::value>::type>::type firebase::firestore::jni::Env::CallHelper<firebase::firestore::DocumentChangeTypeInternal, _jobject* (_JNIEnv::*&)(_jobject*, _jmethodID*, ...), _jobject*, _jmethodID*>(_jobject* (_JNIEnv::*&&&)(_jobject*, _jmethodID*, ...), _jobject*&&, _jmethodID*&&)(enable_if<!:62)
        at libFirebaseCppApp-7_0_1.firebase::firestore::jni::ResultTypeMap<firebase::firestore::DocumentChangeTypeInternal, IsPrimitive<firebase::firestore::jni::JniTypeMap<std::__ndk1::decay<firebase::firestore::DocumentChangeTypeInternal>::type>::type>::value>::type firebase::firestore::jni::Env::Call<firebase::firestore::DocumentChangeTypeInternal>(firebase::firestore::jni::Object const&, firebase::firestore::jni::Method<firebase::firestore::DocumentChangeTypeInternal> const&)(DocumentChangeTypeInternal>:46)
        at libFirebaseCppApp-7_0_1.firebase::firestore::DocumentChangeInternal::type() const(type:30)
        at Firebase.Firestore.FirestoreCppPINVOKE.DocumentChangeProxy_type (System.Runtime.InteropServices.HandleRef)(Native Method)
        at Firebase.Firestore.DocumentChangeProxy.type ()(0x2b:43)
        at Firebase.Firestore.DocumentChange.get_ChangeType ()(0x23:35)
        at CorrespondenceMode.LoadMatch (Firebase.Firestore.QuerySnapshot)(0x7b:123)
        at Firebase.Firestore.Query.<Listen>c__AnonStorey0.<Listen>c__AnonStorey1.<>m__0 ()(0x63:99)
        at Firebase.Dispatcher.Run<TResult_REF> (System.Func`1<TResult_REF>)(0x63:99)
        at Firebase.Platform.FirebaseHandler.RunOnMainThread<TResult_REF> (System.Func`1<TResult_REF>)(0x53:83)
        at Firebase.Firestore.Query.<Listen>c__AnonStorey0.<>m__0 (Firebase.Firestore.QuerySnapshotProxy,Firebase.Firestore.FirestoreError,string)(0xe7:231)
        at Firebase.Firestore.Query.QuerySnapshotsHandler (int,intptr,Firebase.Firestore.FirestoreError,string)(0x8b:139)
        at Firebase.Firestore.Query.QuerySnapshotsHandler (int,intptr,Firebase.Firestore.FirestoreError,intptr)(Native Method)
        at Firebase.AppUtilPINVOKE.PollCallbacks ()(Native Method)
        at Firebase.AppUtil.PollCallbacks ()(0x7:7)
        at Firebase.Platform.FirebaseAppUtils.PollCallbacks ()(0xf:15)
        at Firebase.Platform.FirebaseHandler.Update ()(0x47:71)
        at Firebase.Platform.FirebaseMonoBehaviour.Update ()(0x57:87)
        at System.Object.runtime_invoke_void__this__ (object,intptr,intptr,intptr)(Native Method)

```

ものの見事にクラッシュします。


## 対応策

じゃぁどうすんだって話ですが、現在[こちらのissue](https://github.com/firebase/quickstart-unity/issues/889)で対応中で、次回のリリースで取り込まれるようです。

もし7.0.1をどうしても使わないといけないようでしたら、一旦は下記のコードで回避可能です。

```C#
public DocumentChange.Type GetChangeType(DocumentChange change) {
  if (change.OldIndex < 0) {
    return DocumentChange.Type.Added;
  } else if (change.NewIndex < 0) {
    return DocumentChange.Type.Removed;
  } else {
    return DocumentChange.Type.Modified;
  }
}
```

取り込まれるまでは一旦上記の実装でがんばります。

早くリリースされるのを待ちましょう。