5

So +(id)dataWithContentsOfMappedFile:(NSString *)path is apparently deprecated since iOS 5.0. It sounds to me like I should avoid using it, but then what should I use instead?

I was using mmap to create memory mapped files and it worked with iOS5, but in iOS6, something is wrong because I get an error as soon as I try to update or read the buffer.

  int fd = open(path, O_RDWR);
  off_t offset = 0;
  snapshotData = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
  close(fd);
mprivat
  • 21,582
  • 4
  • 54
  • 64

1 Answers1

10

Use +dataWithContentsOfFile:options:error:. Pass NSDataReadingMappedIfSafe as the option. You can also use NSDataReadingMappedAlways instead, but I recommend the former unless it really has to be mapped. If it really must be mapped, NSDataReadingMappedAlways is still just a hint, so there's no promise. To get a promise, you need to write it yourself, as discussed at CIMG.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I'll try to implement as suggested at CIMG. Thanks for the pointer. – mprivat Sep 27 '12 at 15:07
  • Reading the CIMG comments suggest that the naming of these options could be clearer. `NSDataReadingMappedIfSafe` will do what the deprecated API was doing. `NSDataReadingMappedAlways` will read the entire file into memory (_not usually desired if reaching out for a mapped file_). – mm2001 Feb 23 '20 at 22:53