草庐IT

ios - Firebase 可以在 iOS 7 后台发送和接收吗?

coder 2023-09-21 原文

我在 iOS 7 上的 Objective C 应用程序在后台从 startUpdatingsignificantLocationChanges 或 startUpdatingLocation 委托(delegate)获取位置更新(哪个取决于应用程序所处的模式,但我认为这不重要)。

在委托(delegate)中,我收集位置信息,将其写入字典,然后将字典写入 Firebase。

// this code is in the location update delegate routine

// the code that gathers the various elements that go into the dictionary
// are omitted for clarity, I don't think that they matter

// we may be running in the background on iOS 7 when we are called!

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
   [[NSNumber numberWithFloat:newLocation.coordinate.latitude] stringValue], @"Latitude",
   [[NSNumber numberWithFloat:newLocation.coordinate.longitude] stringValue], @"Longitude",
   [[NSNumber numberWithFloat:newLocation.horizontalAccuracy] stringValue], @"Accuracy",
   formattedDateString, @"TimeNow",
   [dateFormatter stringFromDate:newLocation.timestamp], @"TimeStamp",
   [[NSNumber numberWithDouble:interval] stringValue], @"Date",
   self.mode, @"Mode",
   nil];

   // Write it once to CurrentLocation
   [ref setValue:dictionary];

   // yes, I know this is clumsy
   fbTmp = [NSMutableString stringWithString: fbRoot];
   [fbTmp appendString : @"/locationHistory"];
   ref = [[Firebase alloc] initWithUrl:fbTmp]; 

   // Now write it again to the locationHistory list
   ref = [ref childByAutoId];
   [ref setValue:dictionary];

有时有效,有时无效(即在应用程序的同一次运行中,有时位置会按预期成功写入 Firebase,有时却无效。没有任何明显的规律或原因到什么时候它似乎有效,什么时候无效)。

我怀疑问题是 Firebase 写入在后台模式下未成功完成,但我不确定。我是 iOS、Objective C 和 Firebase 的新手。

我的应用在其功能中被标记为需要位置更新和后台获取的后台服务(后者是我随机尝试解决此问题的,前者我知道我需要)。

我怀疑我需要告诉操作系统我需要时间用 backkgroundTask 完成写入,然后在 firebase 写入的完成 block 中终止后台任务 - 有没有人验证过在运行时会起作用后台模式?

如果是这样,我是否只需要在第二次写入(假设它们按顺序完成)或两者都执行此操作(每次写入完成时我都会倒计时)?

非常感谢任何提示。

最佳答案

是的,您需要在后台完成您的任务。它在Apple Documentation中这样说:

If your app is in the middle of a task and needs a little extra time to complete that task, it can call the beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: method of the UIApplication object to request some additional execution time. Calling either of these methods delays the suspension of your app temporarily, giving it a little extra time to finish its work. Upon completion of that work, your app must call the endBackgroundTask: method to let the system know that it is finished and can be suspended.

只需将您的代码放在后台任务中,给它最大时间(我想是 3 分钟)。

继续阅读 Apple app lifecycle一切都应该清楚,以供将来引用。

关于ios - Firebase 可以在 iOS 7 后台发送和接收吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25954406/

有关ios - Firebase 可以在 iOS 7 后台发送和接收吗?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  3. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  4. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

  5. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  6. ruby - 有人可以帮助解释类创建的 post_initialize 回调吗 (Sandi Metz) - 2

    我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法

  7. ruby - 是否可以覆盖 gemfile 进行本地开发? - 2

    我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI

  8. ruby - 我可以将我的 README.textile 以正确的格式放入我的 RDoc 中吗? - 2

    我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:

  9. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  10. jquery - 我的 jquery AJAX POST 请求无需发送 Authenticity Token (Rails) - 2

    rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送

随机推荐