Skip to content

fix(ios): remove <Images> images from the style on unmount and drop stale map entries - #4424

Merged
mfazekas merged 1 commit into
mainfrom
t3/ios-images-removal-stale-entries
Oct 28, 2026
Merged

mfazekas merged 1 commit into
mainfrom
t3/ios-images-removal-stale-entries

Conversation

@mfazekas

Copy link
Copy Markdown
Contributor

Description

Found while reviewing #4423. Two iOS <Images> bugs:

  1. Removing an <Image> child, or unmounting <Images>, never removed its images from the style, so symbol layers kept drawing them. Removal now deletes the names from the style, matching what Android does when <Images> unmounts (Android doesn't remove images for a single <Image> child either; iOS now does). Images are only removed on view removal, not on style changes.
  2. addToMap appended the component to RNMBXMapView.images on every style load, and removeFromMap never took it out. The list grew by one per <Images> per style switch and on every remount, and it kept unmounted components alive. removeFromMap now removes it.

Shared names: if two <Images> provide the same name and one is removed, the image is removed from the style. The resulting styleImageMissing event then makes the remaining provider re-add it. For that to work with <Image> children, addMissingImageToStyle now also looks at imageViews. I went with this instead of ref-counting because it reuses the existing missing-image path and needs no shared bookkeeping.

Known limitation: the Mapbox renderer keeps drawing an icon that symbols had already laid out with a removed image, even though style.imageExists returns false and the tile re-lays out (the moved points in the reproducer still showed it). It goes away once a referenced image changes or the style reloads; I didn't find an API to force it. This is why the single-child case below still shows the icon visually even though the style state is correct.

Verified on an iPhone 17 simulator (iOS 26.5) using the reproducer below plus temporary logging (not part of this PR), on main (44d37d2) vs. this branch:

  • map.images count over mount → unmount/remount A → 4 style switches: main 2 → 3 → 11 and no RNMBXImages deinit; fixed 2 → 1 → 2 → stays 2, and deinit runs on unmount.
  • imageExists 1s after removing <Image child-red>: main true, fixed false.
  • Unmount <Images> A + reload shape: main still draws red and prop-pin; fixed shows neither, and shared stays (re-added by B).
  • Unmount <Images> B + reload shape: main still draws only-b; fixed removes it, and shared stays (re-added by A).

yarn lint and yarn test pass, and the iOS example builds.

Checklist

  • I've read CONTRIBUTING.md
  • I updated the doc/other generated code with running yarn generate in the root folder
  • I have tested the new feature on /example app.
    • In V11 mode/ios
    • In New Architecture mode/ios
    • In V11 mode/android
    • In New Architecture mode/android
  • I added/updated a sample - if a new feature was implemented (/example)

Component to reproduce the issue you're fixing

Repro.tsx
import React, { useState } from 'react';
import { Button, SafeAreaView, Text, View } from 'react-native';
import {
  Camera,
  Image,
  Images,
  MapView,
  ShapeSource,
  SymbolLayer,
} from '@rnmapbox/maps';

const styleJSON = (color: string) =>
  JSON.stringify({
    version: 8,
    sources: {},
    layers: [
      { id: 'bg', type: 'background', paint: { 'background-color': color } },
    ],
  });
const STYLES = [styleJSON('#eeeeee'), styleJSON('#cccccc')];

const point = (id: string, icon: string, lon: number, lat: number) => ({
  type: 'Feature' as const,
  id,
  properties: { icon },
  geometry: { type: 'Point' as const, coordinates: [lon, lat] },
});

const shape = {
  type: 'FeatureCollection' as const,
  features: [
    point('child', 'child-red', -0.02, 0.02),
    point('prop', 'prop-pin', 0.02, 0.02),
    point('sharedA', 'shared', -0.02, -0.02),
    point('onlyB', 'only-b', 0.02, -0.02),
  ],
};

const Square = ({ color }: { color: string }) => (
  <View style={{ width: 40, height: 40, backgroundColor: color }} />
);

export default function Repro() {
  const [childImage, setChildImage] = useState(true);
  const [imagesA, setImagesA] = useState(true);
  const [imagesB, setImagesB] = useState(true);
  const [styleIndex, setStyleIndex] = useState(0);
  const [styleSwitches, setStyleSwitches] = useState(0);
  const [shapeVersion, setShapeVersion] = useState(0);

  return (
    <SafeAreaView style={{ flex: 1 }}>
      <MapView style={{ flex: 1 }} styleJSON={STYLES[styleIndex]}>
        <Camera centerCoordinate={[0, 0]} zoomLevel={11} animationDuration={0} />
        {imagesA && (
          <Images images={{ 'prop-pin': require('./assets/example.png') }}>
            {childImage && (
              <Image name="child-red">
                <Square color="red" />
              </Image>
            )}
            <Image name="shared">
              <Square color="green" />
            </Image>
          </Images>
        )}
        {imagesB && (
          <Images>
            <Image name="shared">
              <Square color="green" />
            </Image>
            <Image name="only-b">
              <Square color="blue" />
            </Image>
          </Images>
        )}
        <ShapeSource
          id="points"
          shape={{
            ...shape,
            features: shape.features.map((f) => ({
              ...f,
              properties: { ...f.properties, v: shapeVersion },
              geometry: {
                ...f.geometry,
                coordinates: [
                  f.geometry.coordinates[0] + shapeVersion * 0.003,
                  f.geometry.coordinates[1],
                ],
              },
            })),
          }}
        >
          <SymbolLayer
            id="icons"
            style={{
              iconImage: ['get', 'icon'],
              iconAllowOverlap: true,
              iconSize: 1,
            }}
          />
        </ShapeSource>
      </MapView>
      <View style={{ padding: 8 }}>
        <Text testID="state">
          child-red:{String(childImage)} imagesA:{String(imagesA)} imagesB:
          {String(imagesB)} styleSwitches:{styleSwitches}
        </Text>
        <Button
          testID="toggleChild"
          title="Toggle <Image child-red>"
          onPress={() => setChildImage((v) => !v)}
        />
        <Button
          testID="toggleA"
          title="Toggle <Images> A (prop-pin, child-red, shared)"
          onPress={() => setImagesA((v) => !v)}
        />
        <Button
          testID="toggleB"
          title="Toggle <Images> B (shared, only-b)"
          onPress={() => setImagesB((v) => !v)}
        />
        <Button
          testID="reloadShape"
          title="Reload shape (forces symbol relayout)"
          onPress={() => setShapeVersion((v) => v + 1)}
        />
        <Button
          testID="switchStyle"
          title="Switch style"
          onPress={() => {
            setStyleIndex((i) => 1 - i);
            setStyleSwitches((n) => n + 1);
          }}
        />
      </View>
    </SafeAreaView>
  );
}

…tale map entries

Removing an <Image> child or unmounting <Images> now removes its images from the style, as Android does for <Images>. Another <Images> that still provides a removed name re-adds it via addMissingImageToStyle, which now also covers <Image> children.

removeFromMap also drops the component from RNMBXMapView.images; addToMap appended it on every style load, so stale entries piled up and kept unmounted views alive.
@mfazekas
mfazekas marked this pull request as ready for review October 28, 2026 20:49
@mfazekas
mfazekas merged commit b42e5ed into main Oct 28, 2026
19 of 20 checks passed
@mfazekas
mfazekas deleted the t3/ios-images-removal-stale-entries branch October 28, 2026 20:50

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant