A small Android library to manage one-off operations for API 14 and higher.
Some things should happen once.
Once
provides a simple API to track whether or not your app has already performed an action within a given scope.
First things first, you'll need to initialise Once on start up. In your Application
class's onCreate()
override add the follow:
Once.initialise(this);
Now you're ready to go. Say you wanted to navigate to a 'WhatsNew' Activity every time your app is upgraded:
String showWhatsNew = "showWhatsNewTag";
if (!Once.beenDone(Once.THIS_APP_VERSION, showWhatsNew)) {
startActivity(new Intent(this, WhatsNewActivity.class));
Once.markDone(showWhatsNew);
}
Or if you want your app tour to be shown only when a user install, simply check the tag using the THIS_APP_INSTALL
scope:
if (!Once.beenDone(Once.THIS_APP_INSTALL, showAppTour)) {
...
Once.markDone(showAppTour);
}
Your app operations can also be rate-limited by time spans. So for example if you only want to phone back to your server a maximum of once per hour, you'd do the following:
if (!Once.beenDone(TimeUnit.HOURS, 1, phonedHome) { ... }
If checking by time bounds is not enough you can manually get the Date
of last time a tag was marked done by:
Date lastDone = Once.lastDone(brushedTeeth);
This will return null if the tag has never been marked as done.
Say one part of your app triggers functionality elsewhere. For example you might have some advanced feature onboarding to show on the main activity, but you only want to show it once the user has seen the basic functionality.
// in the basic functionality activity
Once.toDo(Once.THIS_APP_INSTALL, "show feature onboarding");
...
// back in the home activity
if (Once.needToDo(showAppTour)) {
// do some operations
...
// after task has been done, mark it as done as normal
Once.markDone(showAppTour);
}
When a task is marked done it is removed from the set of tasks 'to do' so subsequent needToDo(tag)
calls will return false
. To stop the tag from being added back to your todo list each time the user looks at the basic functionality task, we've added a scope to the todo call: toDo(Once.THIS_APP_INSTALL, tag)
. You could also use the THIS_APP_VERSION
scope for todo's which should happen once per app version, or leave off scope complete for tasks which should be repeated every time.
Sometimes you need to keep track of how many times something has happened before you act on it. For example, you could prompt the user to rate your app after they've used the core functionality three times.
// Once again in the basic functionality activity
Once.markDone("action");
if (Once.beenDone("action", Amount.exactly(3))) {
showRateTheAppDialog();
}
You can also change the count checking from exactly(int x)
times, to either Amount.lessThan(int x)
times or Amount.moreThan(int x)
times. When you don't specific a particular amount, Once will default to Amount.moreThan(0)
i.e. checking if it's ever been done at all.
To de-noise your code a bit more you can also static-import the Once
methods, so usage looks a bit cleaner
import static jonathanfinerty.once.Once.THIS_APP_INSTALL;
import static jonathanfinerty.once.Once.beenDone;
import static jonathanfinerty.once.Once.markDone;
...
...
if (!beenDone(THIS_APP_VERSION, tagName)) {
...
markDone(showWhatsNew);
}
Add a library dependency to your app module's build.gradle
:
dependencies {
compile 'com.jonathanfinerty.once:once:1.3.1'
}
You'll need to have mavenCentral()
in your list of repositories
Try out the sample app here: https://play.google.com/store/apps/details?id=jonathanfinerty.onceexample and have a look at it's source code in once-example/
for more simple usage.
Once
was made in '20%' time at Huddle, where its used to help build our Android apps. Pete O'Grady and Paul Simmons also provided invaluable feedback.
Pull requests and github issues are more than welcome and you can get in touch with me directly @jonfinerty.
Copyright 2021 Jon Finerty
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
一、概述 $once是一个函数,可以为Vue组件实例绑定一个自定义事件,但该事件只能被触发一次,触发之后随即被移除。 二、实例 $once有两个参数,第一个参数为字符串类型,用来指定绑定的事件名称,第二个参数设置事件的回调函数。 <template> <div> <button @click="$emit('jpf')">按钮</button> </div> </template>
在vue项目通常我们清理定时器的方法时,通常有两种方法 方法一: 1、首先在vue实例的data中定义定时器的名称: export default{ data(){ timer:null } } 2、在方法(methods)或者页面初始化(mounted())的时候使用定时器 this.timer = setInterval(()=>{ //需要做的事情 },1000);
在Linux内核代码中,经常可以看到读取一个变量时,不是直接读取的,而是需要借助一个叫做READ_ONCE的宏;同样,在写入一个变量的时候,也不是直接赋值的,而是需要借助一个叫做WRITE_ONCE的宏。 代码分析 READ_ONCE宏定义如下(代码位于include/linux/compiler.h中): #define __READ_ONCE(x, check) \ ({
在多线程的环境下,有些时候我们不需要某个函数被调用多次或者某些变量被初始化多次,它们仅仅只需要被调用一次或者初始化一次即可。很多时候我们为了初始化某些数据会写出如下代码,这些代码在单线程中是没有任何问题的,但是在多线程中就会出现不可预知的问题。 bool initialized = false; void foo() { if (!initialized) { do_ini
文本介绍sync.Once的用法及实现原理。 Golang sync.Once实现让方法仅一次的对象。它是Go包内置的功能,角色与init函数类似,但也有差异: init函数是当文件被加载时执行,仅执行一次 sync.Once当代码需要被执行时执行,但也仅执行一次 当一个函数在程序启动后不想被多次执行,我们可以使用sync.Once。 示例 首先从示例开始,看如何使用sync.Once: pack
流计算引擎对状态一致性的保证:Exactly Once, At least Once, At most once 参考: 《Stream processing with Apache Flink》、《Streaming Systems》 流计算引擎一般提供的是系统内部的状态一致性。这其中不包括输入和输出的一致性,因为输入端和输出端通常并不属于流引擎,而会对接其他成熟的系统比如Kafka或数据库。输
exactly-once:即使producer重试发送消息,消息也会保证最多一次地传递给最终consumer。该语义是最理想的,但也难以实现,这是因为它需要消息系统本身与生产和消费消息的应用程序进行协作。例如如果在消费消息成功后,将Kafka consumer的偏移量rollback,我们将会再次从该偏移量开始接收消息。这表明消息传递系统和客户端应用程序必须配合调整才能实现excactly-onc
问题内容: 我想减少我们的构建(使用ant)运行测试所花费的时间。目前,我使用的是default ,它 在每个测试类 () 上派生一个新的虚拟机 。 我正在考虑改用,但 不确定是否会 以某种方式 耦合测试, 并可能在运行测试后给我假阳性和/或假阴性结果。 问题: 每个测试用例是否都会获得一个新的ClassLoader, 以便不再可以访问/查看以前运行的所有静态引用? 是否还有 其他 导致测试依赖/
问题内容: 我正在尝试在onCreate()事件中使用Java API从Android应用程序中的Firebase数据库读取数据。换句话说,我正在尝试做最简单的阅读,相当于… …在Javascript API中。我正在尝试使用addEventListenerForSingleValueEvent()方法,但似乎要我重写onDataChange()方法,这不是我想要的。我想在程序执行到此行时获取数据
问题内容: When I shoot more than 1 bullet fires and I only want 1 bullet to fire at a time (but not only 1 bullet on the screen) They all fire in a large clump and stick together also so I want them to fi
本文向大家介绍实现一个once函数,传入函数参数只执行一次相关面试题,主要包含被问及实现一个once函数,传入函数参数只执行一次时的应答技巧和注意事项,需要的朋友参考一下 参考回答:
本文向大家介绍v-once的使用场景有哪些?相关面试题,主要包含被问及v-once的使用场景有哪些?时的应答技巧和注意事项,需要的朋友参考一下 v-once 只渲染元素和组件一次。随后的重新渲染,元素/组件及其所有的子节点将被视为静态内容并跳过。这可以用于优化更新性能。
本文向大家介绍全面了解#pragma once与 #ifndef的区别,包括了全面了解#pragma once与 #ifndef的区别的使用技巧和注意事项,需要的朋友参考一下 为了避免同一个文件被include多次 1 #ifndef方式 2 #pragma once方式 在能够支持这两种方式的编译器上,二者并没有太大的区别,但是两者仍然还是有一些细微的区别。 方式一: #ifndef _
我知道我做错了什么,因为这很奇怪,我学习PHPUnit已经有几天了。测试主题是控制器操作: 当控制器抛出事件时,我需要模拟一个事件订阅服务器(实现)。有一个静态方法。测试方法帮助程序(获取模拟):
本文向大家介绍vue学习笔记之动态组件和v-once指令简单示例,包括了vue学习笔记之动态组件和v-once指令简单示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了vue动态组件和v-once指令。分享给大家供大家参考,具体如下: 点击按钮时,自动切换两个组件 <component :is="type"></component>,当点击按钮之后,会自动清除原来的组件,显示新的组件。