首先取的,避免存儲大斑點的核心數據,保存縮略圖沒問題(雖然你應該優化你的模型),但你應該存儲完整的IM在「文檔」文件夾中重新構建它的年齡。
你一定要使用隊列,要麼NSOperationQueue或ASI網絡隊列。我在有多個依賴項的應用程序中做類似的事情。因此,對於每個30個對象,你需要有一個塊(或工作功能)時,15幅圖像下載被調用。理想情況下,您希望從主線程中完成這項工作。把所有這些要求放在一起,我會說你至少需要兩個隊列,一個用於網絡請求,另一個用於工作塊,你應該使用NSBlockOperations,這使得整個事情變得更容易。因此,代碼將是這樣的......
// Loop through the objects
for (NSArray *objectParts in objectsToDownload) {
// Create our Object
Object *obj = [Object insertIntoManagedObjectContext:self.moc];
// This is the block which will do the post processing for the object
NSBlockOperation *processBlock = [NSBlockOperation blockOperationWithBlock:^{
// Do post processing here, be very careful with multi-threading CoreData
// it's likely you'll need some class to dispatch you MOCs which have all
// all the observers set up.
// We're gonna assume that all the sub-images have been stored in a instance
// variable:
[obj performPostProcessing];
}];
// Given the list of 15 images which form each object
for (NSURL *part in objectParts) {
// Create the ASI request for this part
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:part];
// Configure the request
[request setDelegate:self];
[request setDidFailSelector:@selector(partRequestDidFail:)];
[request setDidFinishSelector:@selector(partRequestDidFinish:)];
// Store the object in the UserInfo dictionary
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:obj, @"Object", nil];
[request setUserInfo:userInfo];
// Add it as a dependency
[processBlock addDependency:request];
// Add it to our network queue
[networkQueue addOperation:request];
}
// Add the processBlock to our worker queue
[workerQueue addOperation:processBlock];
}
然後你還需要編寫委託方法,該didFinish一個會是這個樣子......
- (void)partRequestDidFinish:(ASIHTTPRequest *)request {
// Remember this is one the main thread, so any heavy lifting should be
// put inside a block operation, and queued, which will complicate the
// dependencies somewhat, but is possible.
// Get the result data
NSData *data = [request responseData];
// Get the object that it belongs to from the user info dic
Object *obj = [[request userInfo] objectForKey:@"Object"];
// Keep track of the partial data in the object
[obj storePartialDataForPostProcessing:data];
}
而且所有這些都會進入你的類,它連接到你的服務器並創建你的對象,所以它不是視圖控制器或任何東西,只是一個常規的NSObject子類。它需要有兩個隊列,一個管理對象上下文(以及更可能返回另一個MOC爲您在線程使用的方法,是這樣的:
// Fends a MOC suitable for use in the NSBlockOperations
- (NSManagedObjectContext *)moc {
// Get a blank managed object context
NSManagedObjectContext *aContext = [[UIApplication sharedApplication] managedObjectContext;
[aContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(mergeChangesFromMOC:) name:NSManagedObjectContextDidSaveNotification object:aContext];
return aContext;
}
- (void)mergeChangesFromMOC:(NSNotification *)aNotification {
@try {
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:aNotification];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:[aNotification object]];
}
@catch (NSException * e) {
NSLog(@"Stopping on exception: %@", [e description]);
}
@finally {}
}
你還需要以某種方式監控進度,重新排隊失敗的下載,取消和保存MOC到最後。重新排隊失敗的下載是非常棘手的。無論如何,希望有幫助。
因此,只是爲了澄清,在你的委託方法,你會存儲下載的圖像您的對象上的臨時實例變量。然後,當所有15個依賴關係完成時,您可以訪問該實例變量並完成您的工作。
@ nsx241您是否嘗試將圖像保存到磁盤並僅將文件名存儲在覈心數據中? – 2011-05-20 07:14:56
圖像被保留,因爲我們希望它們也可以脫機使用。這些圖像實際上是根據客戶數據從第三方圖表引擎動態生成的,因此如果我們在線,我們需要向服務器詢問最新信息。 – nsx241 2011-05-20 18:28:16
@ nsx241我的意思是:你將圖像存儲爲核心數據的blob還是將它們保存在磁盤上? – 2011-05-20 18:31:58