App crashes at [__NSCFDictionary setObject:forKey:] in iOS 9 only - ios

While validating for null values ,the crashes with
-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
When I am using all mutable type.(crashing only in iOS 9,other versions of my app in Appstore are working fine)
Can anyone suggest me how to handle this at null condition to setValue for key.
NSMutableArray *tempelementsArray=[[NSMutableArray alloc]init];
if(![[catDic objectForKey:#"menuElementList"] isEqual:#""]){
tempelementsArray = [catDic objectForKey:#"menuElementList"];
if(tempelementsArray != nil && [tempelementsArray count]>0)
{
for (NSInteger j=0; j<tempelementsArray.count; j++) {
NSMutableDictionary *elementDic = [[NSMutableDictionary alloc]init];
elementDic = [tempelementsArray objectAtIndex:j];
[elementDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([obj isKindOfClass:[NSNull class]])
{
[elementDic setValue:#"" forKey:key];//App crashes here when one of the value is NULL
}
} ];
}
with below crash:
*** Terminating app due to uncaught exception NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
*** First throw call stack:
(
0 CoreFoundation 0x00df3a94 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0051be02 objc_exception_throw + 50
2 CoreFoundation 0x00df39bd +[NSException raise:format:] + 141
3 CoreFoundation 0x00d0ed68 -[__NSCFDictionary setObject:forKey:] + 104
4 Foundation 0x001051ba -[NSMutableDictionary(NSKeyValueCoding) setValue:forKey:] + 68
5 coreDataMenuSample 0x000481d9 __33-[ViewController SaveToCoredata:]_block_invoke188 + 217
6 CoreFoundation 0x00df2849 ____NSDictionaryEnumerate_block_invoke417 + 41
7 CoreFoundation 0x00cd5452 CFBasicHashApply + 130
8 CoreFoundation 0x00d12481 __NSDictionaryEnumerate + 273
9 CoreFoundation 0x00d122ed -[NSDictionary enumerateKeysAndObjectsWithOptions:usingBlock:] + 45
10 CoreFoundation 0x00d12235 -[NSDictionary enumerateKeysAndObjectsUsingBlock:] + 53
11 coreDataMenuSample 0x00043e71 -[ViewController SaveToCoredata:] + 6481
12 coreDataMenuSample 0x0004239d -[ViewController viewDidLoad] + 893
13 UIKit 0x0133fd74 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 44
14 UIKit 0x013448c2 -[UIViewController loadViewIfRequired] + 1556
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I have even checked this similar issue Strange “mutating method sent to immutable object” error when adding an object to a mutable array
Saving CLLocation error: Mutating method sent to immutable object

Look at your code, I think the problem is this line
NSMutableDictionary *elementDic = [[NSMutableDictionary alloc]init];
elementDic = [tempelementsArray objectAtIndex:j];
tempelementsArray contains an instance of NSDictionary instead of NSMutableDictionary. Change to this code will help:
NSMutableDictionary *elementDic = [[NSMutableDictionary alloc]initWithDictionary:tempelementsArray[j]];

There are several things wrong with your code :
First it's not because you initialise a variable with a mutable object that subsequent initialisations will be converted to mutable objects. So when you do :
NSMutableDictionary *elementDic = [[NSMutableDictionary alloc]init];
elementDic = [tempelementsArray objectAtIndex:j];
elementDic contains whatever was in the array at index j, so in this case probably an immutable object. You have to make mutable copies of your objects if you want them mutable.
Second, you can't mutate a dictionary while you enumerate it (which is what you are trying to do here). You have to make a mutable copy and mutate the copy while you enumerate the original.
Third, if you expect [catDic objectForKey:#"menuElementList"] to be an array, why do you test if it's equal to an empty string ?!
Here is a fixed version of your code (with modern obj-C syntax which is much easier to read by the way)
NSDictionary *catDic = ...
NSArray *tempElementsArray = catDic[#"menuElementList"];
NSMutableArray *mutableTempElementsArray = [NSMutableArray arrayWithCapacity:tempElementsArray.count];
if (![tempElementsArray isEqual:#""] && tempElementsArray != nil && tempElementsArray.count > 0)
{
for (NSUInteger j = 0; j < tempElementsArray.count; j++)
{
NSDictionary *elementsDic = tempElementsArray[j];
NSMutableDictionary *mutableElementsDic = [NSMutableDictionary dictionaryWithCapacity:elementsDic.count];
[elementsDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (obj == [NSNull null]) {
obj = #"";
}
mutableElementsDic[key] = obj;
}];
[mutableTempElementsArray addObject:mutableElementsDic];
}
}
NSMutableDictionary *mutableCatDic = [NSMutableDictionary dictionaryWithDictionary:catDic];
mutableCatDic[#"menuElementList"] = mutableTempElementsArray;

Related

Firebase remote config crashing (objective c iOS)

I am having trouble fetching information from Firebase RemoteConfig. On app install, I have a language selection screen before the MainController view controller. On the MainController View controller, I am doing a RemoteConfig fetch. It works the first time, when the language selection screen is shown before the MainController view controller. But, from the next time, it crashes on this particular line:
[self.remoteConfig fetchWithExpirationDuration:expirationDuration completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
With the following exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString firstObject]: unrecognized selector sent to instance 0xa0000000000616a2'
*** First throw call stack:
(
0 CoreFoundation 0x000000010d20134b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010cc6221e objc_exception_throw + 48
2 CoreFoundation 0x000000010d270f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000010d186c15 ___forwarding___ + 1013
4 CoreFoundation 0x000000010d186798 _CF_forwarding_prep_0 + 120
5 English 0x0000000107f44e1b -[GIPLocale googleLanguageWithAppleLanguages:] + 33
6 English 0x0000000107f45396 -[GIPLocale recalculateLocale] + 54
7 English 0x0000000107f44c26 -[GIPLocale initWithLanguageMappings:] + 99
8 English 0x0000000107f44b77 __25+[GIPLocale googleLocale]_block_invoke + 41
9 libdispatch.dylib 0x000000010da900cd _dispatch_client_callout + 8
10 libdispatch.dylib 0x000000010da751fc dispatch_once_f + 501
11 English 0x0000000107f44b4c +[GIPLocale googleLocale] + 42
12 English 0x0000000107f42d06 +[RCNDevice deviceLocale] + 31
13 English 0x0000000107f43344 +[RCNDevice hasDeviceContextChanged:projectIdentifier:] + 325
14 English 0x0000000107f3e133 -[RCNConfigFetch fetchAllConfigsWithExpirationDuration:completionHandler:] + 150
15 English 0x0000000107f3577f -[FIRRemoteConfig fetchWithExpirationDuration:completionHandler:] + 77
UPDATE: It doesn't matter if I show the language selection screen or not. It always works on a fresh install and stops working from next launches.
This is my full code I am using.
-(void)viewDidAppear:(BOOL)animated{
//Display select language settings
if (![[NSUserDefaults standardUserDefaults] boolForKey:DISPLAY_LANGUAGE_SETTING])
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:TRUE forKey:DISPLAY_LANGUAGE_SETTING];
[defaults synchronize];
//Display Language Screen
AGSelectLanguageViewController *languageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"AGSelectLanguageViewController"];
self.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:languageViewController animated:NO completion:nil];
}
[self configFireBase];
}
-(void)configFireBase{
// Firebase Configuration
self.remoteConfig = [FIRRemoteConfig remoteConfig];
//Enabling development mode
FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
self.remoteConfig.configSettings = remoteConfigSettings;
//Set default Remote Config values
[self.remoteConfig setDefaultsFromPlistFileName:#"RemoteConfigDefaults"];
[self fetchConfig];
}
- (void)fetchConfig {
_discount_percentage = self.remoteConfig[DISCOUNT_PERCENTAGE].numberValue.floatValue;
long expirationDuration = 3600;
if (self.remoteConfig.configSettings.isDeveloperModeEnabled) {
expirationDuration = 0;
}
[self.remoteConfig fetchWithExpirationDuration:expirationDuration completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
if (status == FIRRemoteConfigFetchStatusSuccess) {
NSLog(#"Config fetched!");
[self.remoteConfig activateFetched];
} else {
NSLog(#"Config not fetched");
NSLog(#"Error %#", error.localizedDescription);
}
}];
}
Where am I wrong? Please help.
The GIPLocale class does some mapping between Google and Apple names for locales, and as part of that it pulls the app's locales from NSUserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:#"AppleLanguages"];
I am guessing that somewhere that defaults entry is getting set to just a string or similar rather than an array - check if you're referencing that string anywhere in your app.

GMSPolygon array iOS crash

I Have an extremely strange issue with the GMSPolygon object.
Just out of nowhere my code crashes with the error "Unrecognized selector send to instance 0x...."
(Yes it worked all day, and suddenly it starts crashing)
I am using an array of Polygons (to keep track of them and update them dynamically) and I initialize them as follows in my -viewDidLoad:
GMSPolygon *myPoly[50];
--
GMSMutablePath *path = [[GMSMutablePath alloc] init];
// set some fake coordinates, initializing a Polygon with an empty path seems to crash as well...
[path addCoordinate:CLLocationCoordinate2DMake(1,0)];
[path addCoordinate:CLLocationCoordinate2DMake(-1,0)];
[path addCoordinate:CLLocationCoordinate2DMake(0,1)];
for (int i=0;i<50;i++) {
myPoly[i] = [GMSPolygon polygonWithPath:path];
myPoly[i].map = nil;
}
Later on in my program I try to acces the object again in the same way, first set it to nil so it will be removed from the map, and then update and if necessary display it again
for (int i=0;i<50;i++) {
myPoly[i] = [GMSPolygon polygonWithPath:path];
myPoly[i].map = nil; <--------- CRASH
// Do other stuf here, update the Polygon data and if needed
// display again as follows:
myPoly[i].map = mapView_;
}
But it seems to crash..
Same thing happens if I put the GMSPolygon in an NSMutable array. Initializing the array is fine, but getting the GMSPolygon out of the array and setting the .map property gives the same crash..
UPDATE:
It seems be caused by the object Memory locations.. If it works fine, memory locations are as follows:
[0] GMSPolygon * 0x1558ed750 0x00000001558ed750 <--- viewDidLoad for-loop
[0] GMSPolygon * 0x1558ed750 0x00000001558ed750 <--- other function for-loop
When it crashes
[0] GMSPolygon * 0x12e7cbf30 0x000000012e7cbf30 <--- viewDidLoad for-loop
[0] GMSPolygon * 0x129d36630 0x0000000129d36630 <--- other function for-loop
The object is only initialized once in the viewDidLoad, nowhere else!
It obviously explains the crash if the objects memory location are different.. but what's happening here?
Any one an idea why?
UPDATE 2, Got Crash log now:
2016-04-14 15:16:49.618 myApp[1130:240689] -[GMSMutablePath setMap:]: unrecognized selector sent to instance 0x1540bb560
2016-04-14 15:16:49.627 myApp[1130:240689] void uncaughtExceptionHandler(NSException *__strong) [Line 354] CRASH: -[GMSMutablePath setMap:]: unrecognized selector sent to instance 0x1540bb560
2016-04-14 15:16:49.688 myApp[1130:240689] void uncaughtExceptionHandler(NSException *__strong) [Line 355] Stack Trace: (
0 CoreFoundation 0x0000000182ebee50 <redacted> + 148
1 libobjc.A.dylib 0x0000000182523f80 objc_exception_throw + 56
2 CoreFoundation 0x0000000182ec5ccc <redacted> + 0
3 CoreFoundation 0x0000000182ec2c74 <redacted> + 872
4 CoreFoundation 0x0000000182dc0d1c _CF_forwarding_prep_0 + 92
5 myApp 0x00000001001115a0 -[mapViewController plotPoly] + 2028
6 myApp 0x000000010012c944 -[mapViewController mapView:didChangeCameraPosition:] + 556
7 CoreFoundation 0x0000000182ec4ae0 <redacted> + 144
8 CoreFoundation 0x0000000182dbc548 <redacted> + 284
9 CoreFoundation 0x0000000182dc0e70 <redacted> + 60
10 myApp 0x0000000100203370 -[GMSDelegateForward forwardInvocation:] + 108
11 CoreFoundation 0x0000000182ec2aa4 <redacted> + 408
12 CoreFoundation 0x0000000182dc0d1c _CF_forwarding_prep_0 + 92
13 myApp 0x0000000100188fec -[GMSMapView updateWithCamera:] + 176
14 Foundation 0x0000000183893ffc <redacted> + 340
15 CoreFoundation 0x0000000182e75124 <redacted> + 24
16 CoreFoundation 0x0000000182e74bb8 <redacted> + 540
17 CoreFoundation 0x0000000182e728b8 <redacted> + 724
18 CoreFoundation 0x0000000182d9cd10 CFRunLoopRunSpecific + 384
19 GraphicsServices 0x0000000184684088 GSEventRunModal + 180
20 UIKit 0x0000000188069f70 UIApplicationMain + 204
21 myApp 0x000000010013dd3c main + 124
22 libdyld.dylib 0x000000018293a8b8 <redacted> + 4)
2016-04-14 15:16:49.700 myApp[1130:240689] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GMSMutablePath setMap:]: unrecognized selector sent to instance 0x1540bb560'
*** First throw call stack:
(0x182ebee38 0x182523f80 0x182ec5ccc 0x182ec2c74 0x182dc0d1c 0x1001115a0
0x10012c944 0x182ec4ae0 0x182dbc548 0x182dc0e70 0x100203370 0x182ec2aa4
0x182dc0d1c 0x100188fec 0x183893ffc 0x182e75124 0x182e74bb8 0x182e728b8
0x182d9cd10 0x184684088 0x188069f70 0x10013dd3c 0x18293a8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
I am stunned now.
The initial initialization works fine, the Polygons are drawn as the should, but as soon as I call the function to redraw the polygons, the crash happens.. and strange enough now I see why it crashes, but I don't understand.. It changes the first 5 array entry's to GMSMutablePath and GMSPolyLine , instead of GMSPolygon ?!, see link to picture below.. And have no idea why, because I am 100% sure the array of GMSPolygon is not touched anywhere else in the mean time..
Picture of of change in Array type
I can't post a comment yet due to my reputation. Just like what Dan said, use NSArray or NSMutableArray to store your objects. Then do your stuff.
UPDATE:
Here are my suggestions:
Add All Exceptions to your breakpoint navigator.
Try testing your code without loop.
Try adding float values as your coordinates.
See example code below:
GMSMutablePath *prettyPoly = [GMSMutablePath path];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(52.506191, 1.83197)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(52.05249, 1.650696)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(51.92225, 1.321106)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(51.996719, 1.219482)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(52.049112, 1.244202)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(52.197507, 1.334839)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(52.519564, 1.801758)];
GMSPolygon *polygon = [GMSPolygon polygonWithPath: prettyPoly];
polygon.fillColor = [UIColor colorWithRed:0 green:0.25 blue:0 alpha:0.3];
polygon.strokeColor = [UIColor greenColor];
polygon.strokeWidth = 5;
polygon.map = self.googleMap;
If everything failed:
- Refer to this doc: https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_polygon#properties
How about you try Polyline class and see this example: https://developers.google.com/maps/documentation/ios-sdk/shapes#add_a_polyline
Last, try seeking help from Google Forum, or from the repo of that library you are using.

Create a new NsmutableArray in ios

Hello I am newbie to ios and objective c. I am working on a demo. In that I am having a for loop for adding values to NSMutableArray. Now this array will be added to NSMutableDictionary. Now inside this for loop i am having an if condition for checking string. I am adding objects to the array in "if" part and want to create a new Array when condition fail. I have tried as below, but in my Dictionary only one array saved. Can anyone help me where i have made mistake?
NSMutableArray *listingCommonArr,sectionTitles,eventDate;
int j=0;
NSString *dt;
NSMutableArray *sectionArray = [[NSMutableArray alloc]init];
sectionTitles = [orderedSet array];
NSLog(#"===listing common array---%d",[listingCommonArr count]);
sectionData = [[NSMutableDictionary alloc]init];
for(int i =0;i<[listingCommonArr count];i++)
{
dt = [[listingCommonArr objectAtIndex:i] objectForKey:#"start_date"];
NSLog(#"====my date at place===%# & my date is====%#",[sectionTitles objectAtIndex:j],dt);
for (int i =0; i < [listingCommonArr count]; i++) {
dt = [[listingCommonArr objectAtIndex:i] objectForKey:#"start_date"];
if ([(NSString *)[sectionTitles objectAtIndex:j] isEqualToString:dt]) {
[sectionArray addObject:listingCommonArr[i]];
//sectionData = #{dt : sectionArray}; // <-- PAY ATTENTION ON THIS LINE, PLEASE
//[sectionData setValue:sectionArray forKey:dt];
sectionData = [#{dt : sectionArray} mutableCopy];
} else {
[sectionData setObject:#"New value" forKey:#"string"];
sectionArray = [[NSMutableArray alloc]init];
j++;
}
}
NSLog(#"====my section DAta is==%#",sectionData);
}
errorTrace
-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7fc6547832a0
2015-12-18 19:39:41.387 iPhoneExploreCayman[41719:13822557] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7fc6547832a0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a551c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010a1e8bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010a5590ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010a4af13c ___forwarding___ + 988
4 CoreFoundation 0x000000010a4aecd8 _CF_forwarding_prep_0 + 120
5 iPhoneExploreCayman 0x000000010604a48c -[iPhoneECListingView GetEventListingData:] + 1884
6 iPhoneExploreCayman 0x000000010605644b -[iPhoneECListingView EventListStart] + 1323
7 iPhoneExploreCayman 0x0000000106055a98 -[iPhoneECListingView EventClicked:] + 2312
8 UIKit 0x0000000108832d62 -[UIApplication sendAction:to:from:forEvent:] + 75
9 UIKit 0x000000010894450a -[UIControl _sendActionsForEvents:withEvent:] + 467
10 UIKit 0x00000001089438d9 -[UIControl touchesEnded:withEvent:] + 522
11 UIKit 0x000000010887f958 -[UIWindow _sendTouchesForEvent:] + 735
12 UIKit 0x0000000108880282 -[UIWindow sendEvent:] + 682
13 UIKit 0x0000000108846541 -[UIApplication sendEvent:] + 246
I'm highly confused about what you actually want to do here, but... after your comments I have completely updated my answer removing the previous assumptions about what actually is supposed to happen.
it seems to me you want(ed) to group a series of words by the first letter. you have posted a list of animals as example, so I worked with those.
you defined the expected output:
NSDictionary *_expectedOutput = #{#"A" :#[#"Affrican cat", #"Assian cat", #"Alsesian fox"], #"B" : #[#"Bear", #"Black Swan", #"Buffalo"], #"C" : #[#"Camel", #"Cockatoo"], #"D" : #[#"Dog", #"Donkey"], #"E" : #[#"Emu"], #"R" : #[#"Rhinoceros"], #"S" : #[#"Seagull"], #"T" : #[#"Tasmania Devil"], #"W" : #[#"Whale", #"Whale Shark", #"Wombat"]};
as you can see my input array is just an unsorted list of random names of animals from your sample output:
NSArray *_input = #[#"Whale Shark", #"Tasmania Devil", #"Affrican cat", #"Bear", #"Seagull", #"Black Swan", #"Whale", #"Wombat", #"Camel", #"Rhinoceros", #"Cockatoo", #"Buffalo", #"Assian cat", #"Alsesian fox", #"Emu"];
I'm working with that input array and I am going to group them by their first letter and build up the output:
NSMutableDictionary *_output = [NSMutableDictionary dictionary];
[[_input sortedArrayUsingSelector:#selector(compare:)] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *_firstLetter = (obj.length > 0 ? [obj substringToIndex:1] : nil);
if (_firstLetter) {
NSMutableArray *_list = [_output valueForKey:_firstLetter];
if (_list == nil) {
_list = [NSMutableArray arrayWithObject:obj];
[_output setValue:_list forKey:_firstLetter];
} else {
[_list addObject:obj];
}
}
}];
that is pretty much it, if you'd log the _output, you can see on the console that is identical to your _expectedOutput collection.
you have sectionData = #{dt : sectionArray}; outside of the if section.
So on each iteration you dictionary will be recreated.
As I think you need the Mutable dictionary for sectionData, and move that string under "else" section
1 initialize mutable dictionary somewhere before the loop:
sectionData = [NSMutableDictionary new];
2 adding current array to dictionary must be under "else" block, also as I understand, you should create new sectionArray:
else
{
[sectionData setObject: sectionArray forKey:dt];
sectionArray = [[NSMutableArray alloc]init];
}
Every time you call sectionData = #{dt : sectionArray}; or anything sectionData is set other values are remove from dictionary and only last pair stays.(you are initializing dictionary every time you call setObject: forKey:)
If you want to create mutable dictionary and add objects to it you should do:
NSMutableDictionary *d2 = [NSMutableDictionary new];//sectionData in your case
[d2 addEntriesFromDictionary:#{#"b":#"c"}];//new key value pair added to d2
[d2 addEntriesFromDictionary:#{#"c":#"d"}];//new key value pair added to d2
[d2 addEntriesFromDictionary:#{#"d":#"e"}];//new key value pair added to d2

Thread Signal SIGABRT Exception

I'm curious to know why I'm receiving an exception on this code and as to what this exception means. When I remove the for loop, as indicated below, it works fine. When I include it, I get an exception.
-(void) loadVenues {
NSString *latLon = #"34.0500, -118.2500"; // approximate latLon of The Mothership (a.k.a Apple headquarters)
NSString *clientID = kCLIENTID;
NSString *clientSecret = kCLIENTSECRET;
//34.0500° N, 118.2500
NSDictionary *queryParams = #{#"ll" : latLon,
#"client_id" : clientID,
#"client_secret" : clientSecret,
#"categoryId" : #"4bf58dd8d48988d1e0931735",
#"v" : #"20140118"};
[[RKObjectManager sharedManager] getObjectsAtPath:#"/v2/venues/search"
parameters:queryParams
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
_venues = mappingResult.array;
NSMutableArray *temp = [(NSArray*)_venues mutableCopy];
for (int i =0; i < _venues.count; i++)
{
NSUInteger index = 0;
for (int j = 0; j <_venues.count; j++)
{
//((MyObjectType *) [myArray objectAtIndex:0]).intProperty = 12345;
if(((Venue *) [temp objectAtIndex:index]).location.distance > 0)
{
Venue *holder= [temp objectAtIndex:index];
[temp removeObjectAtIndex:index];
NSUInteger indexer = index +1;
[temp addObject:[temp objectAtIndex:indexer]];
//replace a with a+1
[temp replaceObjectAtIndex:indexer withObject:holder];
//[temp addObject:holder atIndex: index+1];
//a[i+1]=holder;
index++;
}
else {index++;}
}
}
_venues = temp;
[self.tableView reloadData];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"What do you mean by 'there is no coffee?': %#", error);
}
];
}
I'm getting an exception at this line:
[temp addObject:[temp objectAtIndex:indexer]];
And I'm not sure why.
Here is the error:
2014-07-02 21:43:12.536 CoffeeKit[63581:60b] I restkit:RKLog.m:33 RestKit logging initialized...
2014-07-02 21:43:12.655 CoffeeKit[63581:60b] I restkit.network:RKObjectRequestOperation.m:150 GET 'https://api.foursquare.com/v2/venues/search?categoryId=4bf58dd8d48988d1e0931735&client_id=ZE5VJOPEO1PP3NDFVCM3O2ZUXWDDJRB20XDDGH3OETBKOVZB&client_secret=5LGY2CEASBQZQS5P0LYFICWDKMDOHJJ00F3G24LT4J4DX4X3&ll=34.0500%2C%20-118.2500&v=20140118'
2014-07-02 21:43:13.085 CoffeeKit[63581:f03] I restkit.network:RKObjectRequestOperation.m:220 GET 'https://api.foursquare.com/v2/venues/search?categoryId=4bf58dd8d48988d1e0931735&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&ll=34.0500%2C%20-118.2500&v=20140118' (200 OK / 30 objects) [request=0.4171s mapping=0.0128s total=0.4486s]
2014-07-02 21:43:13.087 CoffeeKit[63581:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 29 beyond bounds [0 .. 28]'
*** First throw call stack:
(
0 CoreFoundation 0x00000001023a0495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001020ff99e objc_exception_throw + 43
2 CoreFoundation 0x0000000102346745 -[__NSArrayM objectAtIndex:] + 213
3 CoffeeKit 0x0000000100002efa __34-[MasterViewController loadVenues]_block_invoke + 602
4 CoffeeKit 0x000000010009310b __66-[RKObjectRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke244 + 91
5 libdispatch.dylib 0x0000000102c9a851 _dispatch_call_block_and_release + 12
6 libdispatch.dylib 0x0000000102cad72d _dispatch_client_callout + 8
7 libdispatch.dylib 0x0000000102c9d3fc _dispatch_main_queue_callback_4CF + 354
8 CoreFoundation 0x00000001023fe289 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
9 CoreFoundation 0x000000010234b854 __CFRunLoopRun + 1764
10 CoreFoundation 0x000000010234ad83 CFRunLoopRunSpecific + 467
11 GraphicsServices 0x00000001033e2f04 GSEventRunModal + 161
12 UIKit 0x0000000100cace33 UIApplicationMain + 1010
13 CoffeeKit 0x0000000100003733 main + 115
14 libdyld.dylib 0x0000000102efe5fd start + 1
15 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
As you can see from your stack, your error is occurring because you attempt to access the object at index 29, which is outside of the array's bounds:
* -[__NSArrayM objectAtIndex:]: index 29 beyond bounds [0 .. 28]
In your case, it's probably arising from this line:
NSUInteger indexer = index +1;
[temp addObject:[temp objectAtIndex:indexer]];
Which I'm assuming you're using to get the value of the next object. Once the array reaches the 28th object, however, you attempt to access the imaginary 29th object.
Bottom line: refactor your code so that you only access objects that exist.
You are getting out of bounds by accessing the NEXT element in the array, 2 things. you can directly use 'j' to access the element (instead of index, so u dont have to increment it urself) and you can change your for from
for (int j = 0; j <_venues.count; j++)
to
for (int j = 0; j <_venues.count-1; j++)
By the way if all you need is sorting, NSMutableArray offers many options:
Rearranging Content
– exchangeObjectAtIndex:withObjectAtIndex:
– sortUsingDescriptors:
– sortUsingComparator:
– sortWithOptions:usingComparator:
– sortUsingFunction:context:
– sortUsingSelector:

CoreData could not fulfill a fault with unmodified relationship

I have a entity A, it has a relationship B.
Here is a array fetched from CoreData:
NSPredicate * predicate = [NSPredicate predicateWithFormat:#"isSynchronized = NO"];
NSManagedObjectContext *context = [NSManagedObjectContext MR_contextForCurrentThread];
NSArray * newWordsToSynchronize = [A MR_findAllWithPredicate:predicate inContext:context];
NSMutableArray * newWordsParameters = [NSMutableArray arrayWithCapacity:[asToSynchronize count]];
[newWordsToSynchronize enumerateObjectsUsingBlock:^(A * a, NSUInteger idx, BOOL * stop) {
NSLog(#"============= may have some problem ============");
NSLog(#"[a hasFaultForRelationshipNamed:#\"b\"] = %d", [a hasFaultForRelationshipNamed:#"b"]);
NSLog(#"a.word = %#", a.word);
NSLog(#"a.b.objectID = %#", a.b.objectID);
NSLog(#"a.managedObjectContext = %#", a.managedObjectContext);
NSLog(#"a.b.managedObjectContext = %#", [a.b managedObjectContext]);
NSLog(#"a.b.managedObjectContext = %#", [a.b managedObjectContext]);
NSLog(#"a.managedObjectContext = %#", a.managedObjectContext);
NSDictionary *paramDictionary = #{
#"nbook" : a.b.name,
#"lang" : a.lang,
...
};
[newWordsParameters addObject:paramDictionary];
}];
The [a.b managedObjectContext] method may return nil when I call it at second time and then the app goes crash.
Crash log from console:
2014-01-20 14:29:14.561 MyApp[10563:2e07] a.b.objectID = 0x1754e940 <x-coredata://DFC4F956-D5AC-4264-8717-C07090CA9547/B/p1>
2014-01-20 14:29:14.561 MyApp[10563:2e07] a.managedObjectContext = <NSManagedObjectContext: 0x1754bc00>
2014-01-20 14:29:14.561 MyApp[10563:2e07] a.b.managedObjectContext = <NSManagedObjectContext: 0x1754bc00>
2014-01-20 14:29:14.562 MyApp[10563:2e07] a.b.managedObjectContext = (null)
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x1754e940 <x-coredata://DFC4F956-D5AC-4264-8717-C07090CA9547/B/p1>''
*** First throw call stack:
(
0 CoreFoundation 0x037495e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x034cc8b6 objc_exception_throw + 44
2 CoreData 0x031a233b _PFFaultHandlerLookupRow + 2715
3 CoreData 0x031a1897 -[NSFaultHandler fulfillFault:withContext:forIndex:] + 39
4 CoreData 0x031a1473 _PF_FulfillDeferredFault + 259
5 CoreData 0x031a12c6 _sharedIMPL_pvfk_core + 70
6 CoreData 0x031e7130 _pvfk_5 + 32
7 MyApp 0x00181b00 __68-[CichangAHTTPEngine synchronizeAsOnCompletion:failure:]_block_invoke + 1760
8 CoreFoundation 0x037435eb __NSArrayEnumerate + 571
9 CoreFoundation 0x03743196 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 102
10 CoreFoundation 0x037430a5 -[NSArray enumerateObjectsUsingBlock:] + 53
11 MyApp 0x0018105b -[CichangAHTTPEngine synchronizeAsOnCompletion:failure:] + 1179
12 MyApp 0x00185d9e __65-[CichangAHTTPEngine downloader:didFinishedDownloadObject:]_block_invoke_3 + 2030
13 libdispatch.dylib 0x040717f8 _dispatch_call_block_and_release + 15
14 libdispatch.dylib 0x040864b0 _dispatch_client_callout + 14
15 libdispatch.dylib 0x04074eeb _dispatch_root_queue_drain + 287
16 libdispatch.dylib 0x04075137 _dispatch_worker_thread2 + 39
17 libsystem_pthread.dylib 0x04412dab _pthread_wqthread + 336
18 libsystem_pthread.dylib 0x04416cce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type _NSCoreDataException
I found that B will be deallocated after first [a.b managedObjectContext] calling.
I think it was mainly caused by Core Data 1550 error (If I do not call [a.b managedObjectContext], Core Data will reports "The operation couldn’t be completed. (Cocoa error 1550.)"). But what may cause 1550 error and how to fix it?
1550 error report:
Error Domain=NSCocoaErrorDomain Code=1550 "The operation couldn’t be completed. (Cocoa error 1550.)" UserInfo=0x167269c0 {NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1550.), Dangling reference to an invalid object.=null, NSValidationErrorObject=<A: 0x16bab420> (entity: A; id: 0x16baae50 <x-coredata://A85EE9AB-97C1-43C1-B92E-A6C906F0C1A8/A/p168> ; data: {
date = "2014-01-21 02:32:00 +0000";
expect = "2014-01-21 02:31:59 +0000";
isSynchronized = 1;
lang = en;
last = "2014-01-21 02:01:59 +0000";
level = 1;
b = "0x16bab460 <x-coredata://A85EE9AB-97C1-43C1-B92E-A6C906F0C1A8/B/p1>";
status = "-1";
studycount = 0;
trans = "v. \U6d4b\U91cf\Uff0c\U6743\U8861 ";
word = measure;
}), NSAffectedObjectsErrorKey=(
"<B: 0x16babd70> (entity: B; id: 0x16bab460 <x-coredata://A85EE9AB-97C1-43C1-B92E-A6C906F0C1A8/B/p1> ; data: <fault>)"
), NSValidationErrorKey=newWordBook, NSValidationErrorValue=<B: 0x16babd70> (entity: B; id: 0x16bab460 <x-coredata://A85EE9AB-97C1-43C1-B92E-A6C906F0C1A8/B/p1> ; data: <fault>), NSValidationErrorShouldAttemptRecoveryKey=true}
If the 1550 error happened, "CoreData could not fulfill a fault" crash will more easer to cause.
“Core Data could not fulfill fault” really means that there was a deletion. There was data in the persistent store. Then managed object representing this data was created. The object was a fault. Then the data in the store was deleted. Then some property of an object was accessed and Core Data tried to fulfill the fault, but couldn’t.
If you’re not doing explicit deletions, then also make sure that implicit deletions are not happening. Deletion behind the scenes could happen when one of your relationships was configured with the cascade deletion rule.
In your example this could happen if the relationship from A to B was configured with cascade rule, and object A was deleted.

Resources