2011-04-23 78 views
0

嘿傢伙,雖然運行通過調試器我看到下面出現時間它設置變量(時間戳和校驗和通過此方法一個接一個地設置,它工作正常時沒有DataFeedManager存在,但在返回到它再次崩潰時,它的時間來設置校驗):變量不是CFString錯誤

debug screen shot

這裏是感興趣的功能:

//sets specified attribute to the passed in value while ensuring that only one instance of the DataFeedManager exists 
-(void)setItemInDFMWhilePreservingEntityUniquenessForItem:(attribute)attr withValue:(id)value { 
    SJLog(@"CoreDataSingleton.m setItemInDFMWhilePreservingEntityUniquenessForItem"); 
    NSError *error; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"DataFeedManager" inManagedObjectContext:[self managedObjectContext]]; 
    [fetchRequest setEntity:entity]; 
    NSUInteger numEntities = [[self managedObjectContext] countForFetchRequest:fetchRequest error:&error]; 

    if (numEntities == NSNotFound) { // ERROR 
     //... 

    } else if (numEntities == 0) { 
     DataFeedManager *dfm = (DataFeedManager *)[NSEntityDescription insertNewObjectForEntityForName:@"DataFeedManager" 
                       inManagedObjectContext:[self managedObjectContext]]; 
     if (attr == checksumAttr) { //BLOCK OF INTEREST 
      NSString *tempVal = [[NSString alloc] initWithString:value]; 
      [dfm setLastUpdateCheckSum:[NSString stringWithString:tempVal]]; 
     } else if (attr == timeStampAttr) { 
      [dfm setTimeStamp:value]; 
     } 
    } else { // more than zero entities 
     if (numEntities == 1) { 
      NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error]; 
      if (attr == checksumAttr) { //BLOCK OF INTEREST 
       NSString *tempVal = [[NSString alloc] initWithString:value]; 
       [[fetchedObjects objectAtIndex:0] setLastUpdateCheckSum:[NSString stringWithString:tempVal]]; //crashes at this line, after successfully going through the previous BLOCK OF INTEREST area 
      } else if (attr == timeStampAttr) { 
       [[fetchedObjects objectAtIndex:0] setTimeStamp:value]; 
      } 
     } else { // ERROR: more than one entity 
      //... 
     } 
    } // else more than zero entities 
    [fetchRequest release]; 
}//setItemInDFMWhilePreservingEntityUniquenessForItem:withValue: 

我已經用//BLOCK OF INTEREST註釋標記了感興趣的區域,並指出發生崩潰的線路(向右滾動以查看它!)。這是錯誤的,從控制檯讀出:

2011-04-22 17:18:10.924 Parking[26783:207] CoreDataSingleton.m setItemInDFMWhilePreservingEntityUniquenessForItem 
2011-04-22 17:18:10.924 Parking[26783:207] -[__NSCFDictionary length]: unrecognized selector sent to instance 0xac34850 
2011-04-22 17:18:10.970 Parking[26783:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary length]: unrecognized selector sent to instance 0xac34850' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x011a0be9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x012f55c2 objc_exception_throw + 47 
    2 CoreFoundation      0x011a26fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x01112366 ___forwarding___ + 966 
    4 CoreFoundation      0x01111f22 _CF_forwarding_prep_0 + 50 
    5 Foundation       0x00c4d1e1 -[NSPlaceholderString initWithString:] + 162 
    6 Foundation       0x00c562c2 +[NSString stringWithString:] + 72 
    7 Parking        0x0000e4d4 -[CoreDataSingleton setItemInDFMWhilePreservingEntityUniquenessForItem:withValue:] + 774 
    8 Parking        0x00008bb4 -[DataUpdater allDataRetrievedWithSuccess:withError:] + 225 
    9 Parking        0x0000952e -[DataUpdater dataDownloadCompleted:forFunc:withData:withError:] + 769 
    10 Parking        0x00010bb5 -[DataRetriever finish] + 432 
    11 Parking        0x00010e75 -[DataRetriever connectionDidFinishLoading:] + 36 
    12 Foundation       0x00c61172 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108 
    13 Foundation       0x00c610cb _NSURLConnectionDidFinishLoading + 133 
    14 CFNetwork       0x0348e606 _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 220 
    15 CFNetwork       0x03559821 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 293 
    16 CFNetwork       0x03559b0f _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 1043 
    17 CFNetwork       0x03484e3c _ZN19URLConnectionClient13processEventsEv + 100 
    18 CFNetwork       0x03484cb7 _ZN17MultiplexerSource7performEv + 251 
    19 CoreFoundation      0x0118201f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 
    20 CoreFoundation      0x010e019d __CFRunLoopDoSources0 + 333 
    21 CoreFoundation      0x010df786 __CFRunLoopRun + 470 
    22 CoreFoundation      0x010df240 CFRunLoopRunSpecific + 208 
    23 CoreFoundation      0x010df161 CFRunLoopRunInMode + 97 
    24 GraphicsServices     0x01414268 GSEventRunModal + 217 
    25 GraphicsServices     0x0141432d GSEventRun + 115 
    26 UIKit        0x0004e42e UIApplicationMain + 1160 
    27 Parking        0x00002698 main + 102 
    28 Parking        0x00002629 start + 53 
) 
terminate called after throwing an instance of 'NSException' 

我相信這是與充分複製字符串(不能設置一個字符串,我沒有自己的店面)。我曾嘗試將[value copy]以及&value(看到這種事情爲別人工作,所以我認爲我會給它一個鏡頭)無濟於事。我現在的方法不應該充分掌握字符串的所有權嗎?我仍然無法弄清楚我做錯了什麼。任何幫助讚賞。謝謝!

回答

5

最佳猜測(基於在this answer部分)是你在一個釋放的對象傳遞作爲value當你調用該方法第二次,或者可能是value是您第二次NSDictionary類通過 - 這是從這段代碼片段中不清楚爲什麼你的方法需要一個類型爲id的參數,然後把它當作NSString的一個實例,但這可能是問題的一部分。

+0

事實證明,我正在分配一個NSString的實例,並且在發佈這個方法之前就被釋放了。謝謝! – Stunner 2011-04-27 08:17:20

1

請注意,您的-setItemInDFMWhilePreservingEntityUniquenessForItem:withValue:接受任意對象的第二個參數(類型id)。

裏面的方法,這樣做:

NSString *tempVal = [[NSString alloc] initWithString:value]; 

除非value是一個Objective-C的字符串,這將你的程序崩潰。事實上,您的崩潰日誌顯示在該特定執行valueNSDictionary。您需要確保valueNSString

此外,請注意,由於您已使用+alloc,因此您擁有指定給tempVal的字符串。不要忘記釋放該字符串。