react-native-maps: React Native MapView crash application when dynamic sub view are added or removed rapidly

Summary

My Application “Air Map Israel” started failing in production on iOS “expo”: “~52.0.27”, “react-native”: “0.76.6”, “react-native-maps”: “1.18.0”,

Failing on AIRMap.m method (void)insertReactSubview:(id<RCTComponent>)subview on last method line: [_reactSubviews insertObject:(UIView *)subview atIndex:(NSUInteger) atIndex]; In some cases due to null subview and some cases the at index is grater than subviews size To fix it i wrapped it with a check

    if (subview != nil) {
        NSUInteger safeIndex = MIN(atIndex, _reactSubviews.count);
        [_reactSubviews insertObject:(UIView *)subview atIndex:safeIndex];
    }

Did the same in method (void)removeReactSubview:(id<RCTComponent>)subview

    if (subview != nil) {
        [_reactSubviews removeObject:(UIView *)subview];
    }

Need a fix for it. Working fine on android

Reproducible sample code

// noinspection JSValidateTypes

import {View, Text, StyleSheet} from "react-native";
import MapView, {Marker} from "react-native-maps";
import {useEffect, useState} from "react";
import {Checkbox} from "expo-checkbox";

let isDebug = true;
let appTimerId = null;
let markersList = [];

export default function App() {

    const [addMarkers, setAddMarkers] = useState(true);
    const [markers, setMarkers] = useState([]);

    const [appTimerTime, setAppTimerTime] = useState(0)

    const startAppTimer = () => {
        if (appTimerId) clearInterval(appTimerId);
        appTimerId = setInterval(() => {
            setAppTimerTime((previousTime) => previousTime + 1);
        }, 1000);
    }

    const onMapReady = async () => {
        isDebug && console.log(new Date(), 'onMapReady');
        startAppTimer();
    };

    function addMarker() {
        markersList.push({
            title: 'Added ' + new Date().toLocaleTimeString(),
            coordinate: {
                latitude: 32.2 + appTimerTime / 10,
                longitude: 34.8 + appTimerTime / 10,
            }
        })
    }

    useEffect(() => {
        addMarker();
        setMarkers(markersList);
    }, [appTimerTime]);

    return (
        <View
            style={styles.header}
        >
            <MapView
                style={{
                    width: "100%",
                    height: "80%",
                }}
                showsUserLocation={true}
                onMapReady={onMapReady}
            >
                {addMarkers && markers?.length > 0 &&
                    markers.map((m, i) => {
                        return (
                            <Marker key={'marker_' + i}
                                    zIndex={99}
                                    coordinate={m.coordinate}
                                    anchor={{x: 0.5, y: 0.5}}
                                    title={m.title}
                            >
                            </Marker>
                        )
                    })
                }
            </MapView>
            <Text style={{
                padding: 10,
                fontWeight: 'bold',
                height: 50,
            }}>
                {'Show Markers'}
            </Text>
            <Checkbox
                value={addMarkers}
                onValueChange={(value) => setAddMarkers(value)}
                color={addMarkers ? 'blue' : undefined}
            />
        </View>
    );
}

const styles = StyleSheet.create({
    header: {
        flex: 1,
        justifyContent: "center",
        alignItems: "center",
        backgroundColor: 'oldlace'
    },
});

Steps to reproduce

This demo Application adds markers to map view dynamically There is a checkbox to enable and disable it

Play with the checkbox and see ios app crashes

Expected result

Markers will be shown or removed from the map based on the checkbox

Actual result

App crashes

React Native Maps Version

1.18.0

What platforms are you seeing the problem on?

iOS (Apple Maps)

React Native Version

0.76.6

What version of Expo are you using?

SDK 52

Device(s)

iPhone 14 pro, iPhone 16 Pro simulator

Additional information

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 7
  • Comments: 16

Commits related to this issue

Most upvoted comments

Hi React native maps contributors

Please have a look into it - the crashes are going up

Until there will be a fix I released today a new version of my APP with the above suggested fix override (Xcode)

PS Expo recommended version is “1.18.0” I also tested with the latest “1.20.1” but problem is still there

Thanks

This is the error stack on iOS (without the suggested fix)

[CoreFoundation] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil’ *** First throw call stack: ( 0 CoreFoundation 0x00000001804b910c __exceptionPreprocess + 172 1 libobjc.A.dylib 0x0000000180092da8 objc_exception_throw + 72 2 CoreFoundation 0x000000018039ee9c -[__NSArrayM insertObject:atIndex:] + 1232 3 mapissue.debug.dylib 0x0000000106924058 -[AIRMap insertReactSubview:atIndex:] + 884 4 mapissue.debug.dylib 0x00000001064a0f88 -[RCTLegacyViewManagerInteropComponentView finalizeUpdates:] + 1312 5 mapissue.debug.dylib 0x00000001064b8e0c _ZL27RCTPerformMountInstructionsRKNSt3__16vectorIN8facebook5react18ShadowViewMutationENS_9allocatorIS3_EEEEP24RCTComponentViewRegistryR41RCTMountingTransactionObserverCoordinatori

  • 2084 6 mapissue.debug.dylib 0x00000001064b85dc _ZZ41-[RCTMountingMan<…>

Someone has a workaround working?

You can find full project in github https://github.com/oferraanan/map-issue