Skip to content

fix(ios): don't send onImageMissing when an <Images> supplied the image - #4464

Open
mfazekas wants to merge 1 commit into
mainfrom
fix/ios-image-missing-foreach-return
Open

mfazekas wants to merge 1 commit into
mainfrom
fix/ios-image-missing-foreach-return

Conversation

@mfazekas

Copy link
Copy Markdown
Contributor

Description

In the iOS styleImageMissing handler, the return inside self.images.forEach { ... } only left the closure. The handler kept checking every <Images> and then sent onImageMissing even when one of them had already added the image. This switches to a for-in loop so the handler returns at the first <Images> that supplies the image, which matches Android (return@OnStyleImageMissingListener). onImageMissing now fires only when no <Images> has the image.

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)

Screenshot OR Video

Reproduced on the iOS simulator (iPhone 17, iOS 26.5). Two <Images> both supply foo. Unmounting B removes foo from the style. The next shape update makes the map report foo missing, and A re-adds it through addMissingImageToStyle.

Before: after 11 B toggles (6 unmounts), onImageMissing fired 6 times for foo, the image A had just re-added. After: same 11 toggles, 0 events for foo. missing-bar, which nobody supplies, is still reported in both builds. Native logging confirmed the map raised styleImageMissing foo 6 times in each run, so only the JS event differs.

Component to reproduce the issue you're fixing

Press "Unmount B" / "Mount B" a few times. On the unfixed build, the foo counter goes up and the result reads FAIL. On the fixed build it stays at 0 and reads PASS.

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

const RED_SQUARE = {
  uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGUlEQVR4nGP4z8DwnxLMMGrAqAGjBgwXAwAwxP4QHCfkAAAAAABJRU5ErkJggg==',
};

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

const makeShape = (n: number): GeoJSON.FeatureCollection => ({
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      properties: { icon: 'foo', n },
      geometry: { type: 'Point', coordinates: [0.005, 0.005] },
    },
    {
      type: 'Feature',
      properties: { icon: 'missing-bar', n },
      geometry: { type: 'Point', coordinates: [0.015, 0.005] },
    },
  ],
});

// <Images> A always supplies 'foo'. <Images> B supplies it too; unmounting B
// removes 'foo' from the style; the shape update then makes the map lay out the
// symbols again and report 'foo' missing, and A re-adds it.
// onImageMissing should only ever fire for 'missing-bar', which nobody supplies.
export default function Repro() {
  const [showB, setShowB] = useState(true);
  const [toggles, setToggles] = useState(0);
  const [counts, setCounts] = useState<Record<string, number>>({});

  const onImageMissing = useCallback((imageKey: string) => {
    console.log(`[Repro] onImageMissing ${imageKey}`);
    setCounts((c) => ({ ...c, [imageKey]: (c[imageKey] ?? 0) + 1 }));
  }, []);

  const fooCount = counts.foo ?? 0;
  const barCount = counts['missing-bar'] ?? 0;

  return (
    <SafeAreaView style={{ flex: 1 }}>
      <Text testID="counts">
        B toggles: {toggles} | onImageMissing foo: {fooCount} | missing-bar:{' '}
        {barCount}
      </Text>
      <Text testID="result">
        {fooCount > 0
          ? 'FAIL: onImageMissing fired for foo, which <Images> A supplies'
          : toggles > 0
            ? 'PASS'
            : 'Toggle B to test'}
      </Text>
      <Button
        title={showB ? 'Unmount <Images> B' : 'Mount <Images> B'}
        onPress={() => {
          setShowB((v) => !v);
          setToggles((t) => t + 1);
        }}
      />
      <MapView style={{ flex: 1 }} styleJSON={styleJSON}>
        <Camera
          centerCoordinate={[0.01, 0.005]}
          zoomLevel={12}
          animationDuration={0}
        />
        <Images images={{ foo: RED_SQUARE }} onImageMissing={onImageMissing} />
        {showB && <Images images={{ foo: RED_SQUARE }} />}
        <ShapeSource id="points" shape={makeShape(toggles)}>
          <SymbolLayer
            id="icons"
            style={{ iconImage: ['get', 'icon'], iconAllowOverlap: true }}
          />
        </ShapeSource>
      </MapView>
    </SafeAreaView>
  );
}

The return inside forEach only left the closure, so every <Images> was still checked and onImageMissing fired even after one of them had added the image. Use a for-in loop so the handler returns, matching Android.

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