Firebase:混合使用C++与Objective-C SDK是否可行?
Great question! Mixing Firebase’s iOS native SDK and C++ SDK is totally doable—you just need to avoid one critical pitfall: duplicate Firebase App initialization. Let’s break this down clearly.
Will There Be Unexpected Behavior?
Only if you accidentally initialize multiple Firebase App instances. Firebase’s core runtime components are shared across SDK flavors, so duplicate initialization can cause conflicts like mismatched auth states, broken database connections, or resource leaks. Stick to reusing a single App instance, and you’ll avoid these issues.
How to Combine Native iOS Initialization with C++ SDK Usage
Your example scenario (initializing via [FIRApp configure] first, then using the C++ SDK) is exactly the right approach—you just need to reuse the existing native App instance instead of creating a new one in C++. Here’s how:
First, initialize Firebase via iOS native code
// In your iOS Objective-C/Swift code [FIRApp configure]; // This sets up the default Firebase App instanceThen, fetch the existing instance in C++ code
Don’t callfirebase::App::Create(firebase::AppOptions())—that would create a duplicate instance. Instead, use:// In your C++ code firebase::App* firebaseApp = firebase::App::GetInstance(); if (firebaseApp != nullptr) { // Use this pointer to initialize C++ Firebase modules firebase::Auth* auth = firebase::Auth::GetAuth(firebaseApp); // ... work with Firestore, Storage, etc., using the same instance }
This works because the Firebase C++ SDK is designed to interoperate with the native SDKs—it will detect the already-initialized App instance and hook into it seamlessly.
Key Notes to Avoid Issues
- Match SDK versions exactly: Make sure your iOS native Firebase SDK and C++ Firebase SDK are on the same version. Version mismatches can lead to hidden compatibility bugs (e.g., mismatched function signatures or internal state conflicts).
- Configure custom options upfront: If you need non-default App options (like a custom Firebase project for a secondary App), set them in the native initialization step. The C++ SDK will inherit these settings when you fetch the instance.
- Test cross-SDK state sync: Verify that changes made in one SDK (e.g., signing in a user via native Auth) are reflected correctly in the other (e.g., the C++ Auth module sees the signed-in user).
内容的提问来源于stack exchange,提问作者Petre-Daniel Puiu

