react-native-maps: [Expo SDK 52] App creashes with error "NSInvalidArgumentException *** -[__NSArrayM insertObject:atIndex:]: object cannot be nil"
Summary
In some way, I think this issue may be related to https://github.com/react-native-maps/react-native-maps/issues/5206 and https://github.com/react-native-maps/react-native-maps/issues/5214
After upgrading to SDK 52, I’m getting an issue when I remove and add new markers.
In my app, I could remove all the code unrelated to this issue, and get this small version of the “Map” component, where I can still reproduce the error. I will add a video in the comments, so you see how to reproduce it (you have to play around with the zoom).
I migrated that code to a Expo Snack, but you won’t be able to reproduce it from the snack, because iOS seems to be broken with SDK 52.
Here is the SNACK: https://snack.expo.dev/@brodanoel/react-native-maps-not-working
Reproducible sample code
import { useState } from 'react';
import MapView, { Marker } from 'react-native-maps';
const getZoom = region => Math.round(Math.log(360 / region.longitudeDelta) / Math.LN2);
const initialRegion = {
latitude: -31.4205589,
latitudeDelta: 0.5,
longitude: -64.1734649,
longitudeDelta: 0.5,
};
const clusters = [
{ coordinate: { latitude: -31.42, longitude: -64.185 }, i: 'aaa' },
{ coordinate: { latitude: -31.439, longitude: -64.128 }, i: 'bbb' },
];
const Map = () => {
const [pointsToShow, setPointsToShow] = useState([]);
const onRegionChange = currentRegion => {
const zoom = getZoom(currentRegion);
if (zoom >= 12) {
setPointsToShow([]);
} else {
setPointsToShow(clusters);
}
};
return (
<MapView
style={{ flex: 1 }}
provider="google"
onRegionChangeComplete={onRegionChange}
initialRegion={initialRegion}
>
{pointsToShow.map(point => {
return <Marker key={point.i} pinColor="red" coordinate={point.coordinate} />;
})}
</MapView>
);
};
export default Map;
Steps to reproduce
You should get the error while zooming in and out. Please wait for the video that I’m gonna add in the comments
Expected result
App not crashing
Actual result
App crashes and get closed
React Native Maps Version
1.18.0 and 1.20.0
What platforms are you seeing the problem on?
iOS (Google Maps)
React Native Version
0.76.2
What version of Expo are you using?
SDK 52 with New Architecture enabled
Device(s)
iPhone 13 mini
Additional information
None
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 10
- Comments: 32 (11 by maintainers)
Same issue paired with react-native-map-clustering
Same thing to me. If you can, just disable the New Arch until this gets fixed.
This is what ChatGPT says
Understanding the Root Cause
Subview is nil: The most probable cause is that subview is nil when you attempt to insert it into _reactSubviews. This can happen if one of the child subviews is nil.
Not Calling super Methods: In your insertReactSubview and removeReactSubview methods, you are overriding the methods without calling the superclass implementation. React Native relies on these methods to manage the view hierarchy correctly.
Solution
Call super Methods: Modify your insertReactSubview and removeReactSubview methods to call the superclass implementation. This ensures that React Native’s internal mechanisms for managing subviews work correctly.
Remove Manual Management of _reactSubviews: Since React Native already manages subviews internally, you don’t need to maintain your own _reactSubviews array.
Explanation
Calling super Methods: By calling [super insertReactSubview:subview atIndex:atIndex];, you ensure that the parent class can perform its necessary operations, such as managing the view hierarchy and state.
Avoiding nil Subviews: Since the superclass method properly handles subviews, it reduces the chances of nil subviews causing errors.
Why This Works
React Native’s view management system expects certain behaviors from native components. When you override methods like insertReactSubview and removeReactSubview without calling the superclass implementations, you can inadvertently disrupt the view hierarchy management, leading to unexpected errors like inserting nil objects.
By updating your methods to call the superclass implementations, you allow React Native to manage the subviews correctly, and you avoid inserting nil objects into arrays.
I’m having similar issues, markers behaving weirdly and app crash on zoom in move around.
The 2 PR created fixes the crash, but they are not actually fixing the whole problem behind.
I’ve created a new build with the patch applying the PR #5226 , and there is 1 issue that still exists, which is clearly the actual error in the behind, that we should actually fix.
If you see my video, when I zoom IN, and then zoom OUT again, the app crashes. After applying the path it doesn’t crash BUT the clusters/markers are not shown until you move the map again.
I’m sharing a video here so you can understand what is still happening: https://drive.google.com/file/d/1VGLSqeXKroGzxJCXXUhCkJVKdP9L74Ko/view?usp=sharing
ChatGPT recommends to replace this line:
With this line:
And letting the superClass handle the subviews… but… I have NO idea what is talking about!
It also recommends to replace
[_reactSubviews removeObject:(UIView *)subview];with[super removeReactSubview:subview];And removing this whole method:
I won’t be able to rest it this week. Can somebody try to patch that change and see it that works?
I’m also getting this issue - it’s been driving me crazy! The only way I was able to get something working was by re-rendering the whole MapView, which led to a poor UX with the map flickering
onRegionChangesame issue
same issue
Still same issue