小编Luk*_*uke的帖子

在后台线程上执行Core Data保存?

我有一个按钮,它将Core Data SQLite中的一个选定条目标记为"收藏夹",这意味着我只是BOOL将该索引从off更改为on.

目前,当我这样做,我叫save上了managedObjectContext,这也许需要500毫秒,更也许有点,根据仪器.

我有一些代码同时执行,触发了一个漂亮的小粒子爆炸("万岁,最喜欢的!"),但我遇到的问题是爆炸延迟到save完成之后.

我不知道为什么,因为触发爆炸的代码是在save调用之前.我是一个相对较新的程序员,所以也许我错过了一些东西,但是在这样的情况下不会逐行执行代码执行,因为爆炸会触发,然后保存将在它发生时发生?这里的委托调用也可能需要一些时间,但同样的问题适用,为什么它会在这些代码行之后重要?

编辑:我是否正确地说,在下一行代码出现粒子之前,主线程被阻塞,这意味着UI无法自行更新?

这是我的代码:

// Particle animation
LikeExplosion *likeExplosionView = [[LikeExplosion alloc] initWithFrame: CGRectMake(0, 0, 320, 400)];
[likeExplosionView setUserInteractionEnabled: NO];
[self.view addSubview: likeExplosionView];
[self.view bringSubviewToFront: likeExplosionView];
[likeExplosionView decayOverTime: 1.1];

// Delegate call to reload tableview elsewhere
[self.delegate detailViewControllerDidLikeLine];

// Update current object
[_selectedLine setIsLiked: [NSNumber numberWithBool: YES]];
[_selectedLine setIsDisliked: [NSNumber numberWithBool: NO]];

// Update context
NSError *error;
if (![[[CDManager sharedManager] managedObjectContext] save:&error]) NSLog(@"Saving changes failed: …
Run Code Online (Sandbox Code Playgroud)

structure core-data objective-c nsmanagedobjectcontext ios

1
推荐指数
2
解决办法
1万
查看次数

Swift 2.0中的NSFileManager fileExistsAtPath?

因此,使用Swift 2.0,看起来Apple正在引导我们使用NSURL而不是NSString路径.

我正在尝试确定iOS中用户的Documents目录中是否存在文件,而且我无法将它拼凑在一起.

如果我使用以下内容,Swift 2.0会抱怨我不应该使用stringByAppendingPathComponent,并且我应该使用URL.

let documents = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let dbPath = documents.stringByAppendingPathComponent(“Whatever.sqlite”)
Run Code Online (Sandbox Code Playgroud)

如果我然后获取URL,如下所示:

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent("Retrollect.sqlite”)
Run Code Online (Sandbox Code Playgroud)

然后我不能打电话NSFileManager.defaultManager().fileExistsAtPath来确定文件是否存在.

是否有相当于fileExistsAtPath()a NSURL,查看用户的Documents目录?

nsfilemanager ios swift2

1
推荐指数
1
解决办法
2796
查看次数

AVAudioPlayer拒绝播放任何内容,但没有错误等

这是最简单的AVAudioPlayer代码,它只是无法播放任何东西.没有错误,控制台根本没有任何东西,文件肯定被发现,好像我将URL字符串更改为不存在的东西,我确实遇到了崩溃.我在这做错了什么?我已经尝试过,无论是否有代表,以及有没有prepareToPlay,我都无法得到任何东西.我也试过各种声音文件.真的在我的头发上撕了我的头发!

@implementation ViewController

- (IBAction)playSound:(id)sender
{
    NSURL *soundLocation = [[NSURL alloc] initWithString:@"Lot01.wav"];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundLocation error:nil];
    [audioPlayer setDelegate:self];
    [audioPlayer play];
}

@end
Run Code Online (Sandbox Code Playgroud)

objective-c avaudioplayer wave

0
推荐指数
1
解决办法
1192
查看次数

当我尝试将NSURL设置为NSString时,NSURL正在被取消

我正在使用以下代码加载一些声音,具体取决于用户点击的按钮.文件路径完全加载testString,但是当NSLogging时URLToBeReturned,它出来了(null).我不确定为什么它没有转移.

NSString *testString = [NSString stringWithString:[[NSBundle mainBundle] pathForResource:noteName ofType:@"m4a"]];
NSURL *URLToBeReturned = [NSURL URLWithString:testString];
return URLToBeReturned;
Run Code Online (Sandbox Code Playgroud)

xcode objective-c nsurl nsstring ios

0
推荐指数
1
解决办法
160
查看次数